-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·69 lines (54 loc) · 1.67 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
var express = require("express");
var AV = require("leanengine");
var path = require("path");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var cloud = require("./cloud.js");
var service = require("./routes/service.js");
var portal = require("./routes/portal.js");
var app = express();
// Set view engine.
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static("public"));
// Loading Cloud function
app.use(cloud);
//app.use(AV.Cloud);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.get("/hello", function(req, res) {
res.json({ currentTime: new Date() });
});
// Defined service routers
app.use("/users", service);
// Defined portal routers
app.use("/portal", portal);
// 如果任何路由都没匹配到,则认为 404
// 生成一个异常让后面的 err handler 捕获
app.use(function(req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handlers
// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
if (app.get("env") === "development") {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: err
});
});
}
// 如果是非开发环境,则页面只输出简单的错误信息
app.use(function(err, req, res, next) {
console.log("It's production environment");
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: {}
});
});
module.exports = app;