-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathapp.js
76 lines (55 loc) · 1.61 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var express = require("express"),
bodyParser = require("body-parser"),
hbs = require("hbs"),
path = require("path"),
cors = require("cors"),
app = express(),
port = process.env.PORT || 5000;
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(cors());
app.set("views", __dirname + "/views");
app.set("view engine", "html");
app.engine("html", hbs.__express);
app.use(express.static(path.join(__dirname, "public")));
var routes = require("./routes/");
app.all("/api/*", [bodyParser(),
function(req, res, next) {
if (req.query && req.query.delay && !isNaN(req.query.delay)) {
return setTimeout(next, req.query.delay * 1000);
}
return next();
}
]);
app.get("/", function(req, res, next) {
res.render("index");
});
app.post("/api/login", routes.login);
app.post("/api/login/", routes.login);
app.post("/api/register", routes.register);
app.post("/api/register/", routes.register);
app.post("/api/logout", routes.logout);
app.post("/api/logout/", routes.logout);
app.get("/api/:resource/*", routes.get);
app.get("/api/:resource", routes.get);
app.post("/api/:resource/", routes.post);
app.post("/api/:resource", routes.post);
app.put("/api/:resource/*", routes.put);
app.delete("/api/:resource/*", routes.delete);
app.use(function(req, res, next) {
res.status(404);
if (req.accepts("html")) {
res.render("404", {
url: req.url
});
return;
}
res.type("txt").send("Not found");
});
var server = app.listen(port, function() {
var host = server.address().address,
port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});