-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
45 lines (40 loc) · 1.25 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
const dotenv = require('dotenv')
dotenv.config()
const express = require('express')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const chalk = require('chalk')
const app = express()
const author = require('./middleware/author')
const router = require('./routes/index')
const PORT = process.env.PORT || 3000
//配置跨域
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Headers', '*')
res.header('Access-Control-Allow-Headers', 'Authorization')
res.header('Access-Control-Allow-Methods', '*')
res.header('Access-Control-Allow-Origin', '*')
res.header('Content-Type', 'application/json;charset=utf-8')
next()
})
//静态资源路径
app.use('/public', express.static('./public'))
app.use(cookieParser())
//配置参数解析
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(author)
//载入路由
router(app)
//异常请求处理
app.use(function(req, res) {
console.log(req.headers)
let err = `The '${req.protocol + '://' + req.hostname + req.originalUrl}' is not found`
res.json({
status: 404,
message: err
})
})
app.listen(PORT, function() {
console.log(chalk.green('the server is running at 3000...'))
})