-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (33 loc) · 1.31 KB
/
Copy pathindex.js
File metadata and controls
39 lines (33 loc) · 1.31 KB
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
const express = require('express')
const app = express()
const session = require('express-session') // session管理
const cors = require('cors') // 解决跨域
const bodyParser = require('body-parser') // 解析POST请求数据
const cookieParser = require('cookie-parser') // 第三方cookie操作模块,方便操作客户端中的cookie值
const path = require('path')
const blog = require('./api/blog.js')
const test = require('./api/test.js')
var allowCors = function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials','true');
next();
};
app.use(allowCors);//使用跨域中间件
app.use(session({
secret: '8023',
resave: false,
saveUninitialized: true
}))
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser())
app.use(express.static(path.join(__dirname, '../dist'))) // 部署上线时读取静态文件
app.use('/api/blog', blog);
app.use('/api/test', test);
app.listen(3000)
console.log('success listen at port:3000......')