-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathserver.js
66 lines (54 loc) · 1.59 KB
/
server.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
const pkg = require('./package')
const config = require('./server/db/dbconfig')
const express = require('express')
const session = require('express-session')
const favicon = require('serve-favicon')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
// const MongoStore = require('connect-mongo')(session);
const index = require('./server/router/index') // localhost:3000/
// session机制的user
const user = require('./server/router/user')
// token机制的user
const usertoken = require('./server-token/router/usertoken')
const db = mongoose.connect(config.mongodb);
// 数据库连接
db.connection.on("error", function(error) {
console.log("数据库连接失败:" + error);
});
db.connection.on("open", function() {
console.log("------数据库连接成功!------");
});
// Use native promises
mongoose.Promise = global.Promise
const app = express()
const port = process.env.PORT || 3000
// url请求解析
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))
// favicon
app.use(favicon(__dirname + '/src/assets/favicon.ico'))
// 静态资源
app.use(express.static('dist'))
// session
app.use(session({
secret: 'usersession',
key: 'usersession',
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 10000 // 设置返回的cookie时效为30秒,测试用
}
// store: new MongoStore({
// url: "mongodb://localhost:27017/usersession"
// })
}))
// 设定路由
app.use('/', index)
app.use('/api', user)
app.use('/api/token', usertoken)
app.listen(port, () => {
console.log(`${pkg.name} listening on port ${port}`)
})