-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (61 loc) · 1.98 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
77
78
//server.js
//BASE SETUP
//====================================
//call the packages we need
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var fs = require('fs');
var path = require('path');
var fileStreamRotator = require('file-stream-rotator');
//call routers modules
var indexRouter = require('./routes/index.js');
//LOG
var logDir = path.join(__dirname, 'logs');
//ensure log directory exists
fs.existsSync(logDir) || fs.mkdirSync(logDir);
//create a rotating write stream
var accessLogStream = fileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: path.join(logDir, 'access-%DATE%.log'),
frequency: 'daily',
verbose: true
});
//APP CONFIG
//====================================
//configure app to use BodyParser();
//this will let us get the data form a POST
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//log module
app.use(morgan('short'));
app.use(morgan('combined', {
stream: accessLogStream
}));
var port = process.env.PORT || 8080; //set port
//REGISTER OUR ROUTES
//===================================
//route 控制 后台接口
app.all("*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.use('/', indexRouter);
//ERROR HANDLE
app.use(function(req, res, next){
res.status(404).send('Not Found');
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
//START THE SERVER
//====================================
app.listen(port, function(){
console.log('server is listening to port ' + port);
});