-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
82 lines (69 loc) · 2.35 KB
/
Copy pathapp.js
File metadata and controls
82 lines (69 loc) · 2.35 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
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
const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const history = require('connect-history-api-fallback')
const { successCode, failureCode, dataNotLegal, requestSucceeded } = require("./routes/routes_config")
// const indexRouter = require('./routes/index')
const usersRouter = require('./routes/users')
const homeRouter = require('./routes/home')
const packageRouter = require('./routes/package')
const app = express()
app.use(history())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
// app.enable('trust proxy');
// app.all("*", (req, res, next) => {
// if (req.secure) {
// // request was via https, so do no special handling
// next();
// } else {
// let host = req.headers.host;
// host = host.replace(/\:\d+$/, ''); // Remove port number
// res.redirect(307, `https://${host}${req.path}`);
// }
// });
app.all("*",function(req,res,next){
//设置允许跨域的域名,*代表允许任意域名跨域
// res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Origin",req.headers.origin);
res.header("Access-Control-Allow-Credentials", true);
//允许的header类型
res.header("Access-Control-Allow-Headers","content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() == 'options') {
res.sendStatus(200); //让options尝试请求快速结束
} else {
next();
}
})
app.use('/avatar', express.static(path.join(__dirname, './resource/avatar')));
app.use('/app', express.static(path.join(__dirname, './resource/app')));
app.use((req, res, next) => {
if (req.secure) {
// request was via https, so do no special handling
next()
} else {
let host = req.headers.host
host = host.replace(/\:\d+$/, '') // Remove port number
res.redirect(307, `https://${host}${req.path}`)
}
});
app.use(express.static(path.join(__dirname, 'public')))
// app.use(indexRouter)
app.use(usersRouter)
app.use(homeRouter)
app.use(packageRouter)
app.use((req, res, next) => {
res.status(404).send('404 Not Found')
});
app.use((err, req, res, next) => {
res.status(500).json({
ret: failureCode,
message: err.message
})
})
module.exports = app