-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (73 loc) · 2.24 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
79
80
import express from 'express'
import './mongodb/db.js'
import config from './config'
import router from './routes/index.js'
// import winston from 'winston'
// import expressWinston from 'express-winston'
import chalk from 'chalk'
var cookieParser = require('cookie-parser')
var https = require('https')
var http = require('http')
var fs = require('fs')
// 同步读取密钥和签名证书
var options = {
key: fs.readFileSync('./keys/server.key'),
cert: fs.readFileSync('./keys/server.crt')
}
// import {
// verify
// } from './auth/auth'
const app = express()
app.use(cookieParser())
app.all('*', (req, res, next) => {
// console.log(req)
// console.log(res)
res.header('Access-Control-Allow-Origin', req.headers.origin || '*')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS,PATCH')
res.header('Access-Control-Allow-Credentials', true) // 可以带cookies
res.header('X-Powered-By', 'kk.5.2.1')
// if (req.path !== 'auth') {
// if (!req.headers.Authorization) {
// res.send({
// status: 0,
// type: 'SAVE_USER_FAILED',
// message: 'NO TOKEN',
// })
// }
// verify(req.headers.Authorization, (err, decode) => {
// if (err) {
// res.json({err:err})
// } else {
// console.log(decode)
// next()
// }
// })
// }
if (req.method === 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
})
router(app)
app.use(express.static('./public'))
app.get('/', function (req, res) {
console.log('Hello World!!')
res.send('Hello World!!')
})
var httpsServer = https.createServer(options, app)
var httpServer = http.createServer(app)
// https监听3000端口
httpsServer.listen(443, () => {
console.log(chalk.blue('Example app listening at http://%s:%s', 443))
})
// http监听3001端口
httpServer.listen(config.port, () => {
console.log(chalk.blue('Example app listening at http://%s:%s', config.port))
})
// var server = app.listen(config.port, () => {
// var host = server.address().address
// var port = server.address().port
// console.log(chalk.blue('Example app listening at http://%s:%s', host, port))
// })