forked from angular-cn/ng-nice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (78 loc) · 3.04 KB
/
server.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
77
78
79
80
81
82
83
/*global require,process,__dirname,console*/
(function () {
"use strict";
var express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
express_layout = require('express3-ejs-layout'),
compression = require('compression'),
http = require('http'),
path = require('path'),
app = express(),
route = require("./route"),
config = require("./config.js"),
app_package = require("./package.json"),
core = require("./core/"),
locals_app = {
title : "AngularJS Nice Things",
version : app_package.version,
env : config.env,
base_url: config.base_url
};
app.use(compression());
app.use(bodyParser());
app.use(methodOverride());
app.use(cookieParser(config.cookie_secret));
app.set('views', __dirname + '/web/view');
app.use(express.static(__dirname + '/web/static'));// {maxAge: 31557600000}
app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express_layout);
app.set('layout', 'layout');
app.set("env", config.env);
app.disable("x-powered-by");
morgan.token('data', function (req) {
return "params:" + JSON.stringify(req.params) + ",query:" + JSON.stringify(req.query) + ",body:" + JSON.stringify(req.body);
});
morgan.token('date', function () {
var now = new Date();
return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
});
morgan.token('operationId', function (req) {
return req.context ? req.context.operationId : "null";
});
if ('development' === config.env) {
app.use(morgan(':method :url :status :remote-addr :data [:date][:response-time ms] [:operationId]'));
locals_app.site_scripts = config.site_scripts;
} else {
app.use(morgan(':method :url :status :remote-addr [:date][:response-time ms] [:operationId]'));
//URL 检查并重定向
app.use(function (req, res, next) {
if (req.headers.host == "angular.duapp.com") {
res.writeHead(301, {'Location': 'http://' + config.host + req.url});
res.end();
return;
}
if (req.headers.host !== config.host) {
return res.redirect('http://' + config.host + req.url);
}
else {
next();
}
});
}
app.locals.app = locals_app;
//初始化路由
route(app);
//create server
app.listen(config.port, function () {
console.log('ng-nice server listening on port ' + config.port + " in env " + config.env);
});
if (config.env === 'production') {
process.on("uncaughtException", function (err) {
console.log("process uncaughtException:" + err);
});
}
})();