-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.25 KB
/
index.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
var path = require('path');
function resolve(url) {
if (process.env.NODE_ENV === 'production') {
return path.join(__dirname, 'build', url);
} else {
return path.join(__dirname, 'src', url);
}
}
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var config = require(resolve('config')); // 文件配置
var routes = require(resolve('routes')); // 路由入口
var app = express();
//解决跨域问题
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8080');
res.header('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials', 'true');
if (req.method === 'OPTIONS') res.send(200); /*让options请求快速返回*/
else next();
});
app.use(cookieParser());
app.use(
session({
resave: true,
saveUninitialized: false,
name: config.session.key,
secret: config.session.secret,
cookie: { maxAge: 10000000 }
})
);
routes(app);
app.listen(config.port, function(err) {
if (err) {
console.error(err);
}
console.log('server run!\n');
});