forked from parox2014/dwy-angular2starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (55 loc) · 1.43 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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const path=require('path');
const favicon = require('serve-favicon');
const server = express();
server.set('view engine', 'ejs');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
extended: false
}));
server.use(express.static(path.join(__dirname, 'static')));
server.use(cookieParser());
server.use(favicon(__dirname + '/static/images/favicon.ico'));
server.use(function (req, res,next) {
var _path=req.path;
console.log(_path);
if(_path.indexOf('/api')>=0){
next();
}else{
res.render('index');
}
});
// catch 404 and forward to error handler
server.use(function(req, res, next) {
var err = new Error('Page Not Found');
err.status = 404;
next(err);
});
// 错误处理
// 开发环镜
if (server.get('env') === 'development') {
server.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
title: err.message,
message: err.message,
error: err
});
});
}
// 生产环境错误处理
server.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
server.locals.base='http://localhost:3000/';
server.listen(3000, function() {
console.log('server start success at port:' + 3000);
});
module.exports = server;