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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| 'use strict';
var express = require('express'); var timeout = require('connect-timeout'); var path = require('path'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser');
var app = express();
// 设置模板引擎,路径在根目录+public中 app.set('views', path.join(__dirname, 'public')); app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.static(path.join(__dirname, 'public')));
// 设置默认超时时间 app.use(timeout('15s')); //请求体 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); //cookie app.use(cookieParser()); //注册HTTP消息头部信息 app.use( function(req, res, next) { res.set( { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Rememberme': true, 'Access-Control-Allow-HttpOnly': false, 'Access-Control-Allow-Methods': 'POST, GET, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Credentials': true, //false, 'Access-Control-Max-Age': '86400', // 24 hours 'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept' } );
//decodeURI(req.url) console.log('%s %s', req.method, req.url); next(); } );
//首页 app.get('/', function(req, res) { res.render('index1', { currentTime: new Date() }); });
app.use(function(req, res, next) { // 如果任何一个路由都没有返回响应,则抛出一个 404 异常给后续的异常处理器 if (!res.headersSent) { var err = new Error('Not Found'); err.status = 404; next(err); } });
// 错误处理 app.use(function(err, req, res, next) { if (req.timedout && req.headers.upgrade === 'websocket') { // 忽略 websocket 的超时 return; }
var statusCode = err.status || 500; if (statusCode === 500) { console.error(err.stack || err); } if (req.timedout) { console.error('请求超时: url=%s, timeout=%d, 请确认方法执行耗时很长,或没有正确的 response 回调。', req.originalUrl, err.timeout); } res.status(statusCode); // 默认不输出异常详情 var error = {}; if (app.get('env') === 'development') { // 如果是开发环境,则将异常堆栈输出到页面,方便开发调试 error = err; } res.render('error', { message: err.message, error: error }); });
function catchGlobalError(err) { // 注册全局未捕获异常处理器 process.on('uncaughtException', function(err) { console.error('Caught exception:', err.stack); }); process.on('unhandledRejection', function(reason, p) { console.error('Unhandled Rejection at: Promise ', p, ' reason: ', reason.stack); }); }
//创建两个服务器实体 var server = require('http').createServer(app); var server1 = require('http').createServer(app);
//服务器监听端口 var PORT = parseInt(process.env.PORT || 3000); var PORT1 = PORT + 1;
server.listen(PORT, function (err) { console.log('Node app is running on port:', PORT); catchGlobalError(err); });
server1.listen(PORT1, function (err) { console.log('Node app is running on port:', PORT1); catchGlobalError(err); });
|