-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (35 loc) · 1.38 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
const express=require('express')
const mongoose=require('mongoose')
const bodyParser=require('body-parser')
const passport=require('passport')
const app=express()
//findOneAndUpdate()内部会使用findAndModify驱动,驱动即将被废弃,所以弹出警告.在使用mongose时全局设置一下
mongoose.set('useFindAndModify', false)
//引入user.js
const users=require('./routes/api/users')
//引入profiles.js
const profiles=require('./routes/api/profiles')
//引入notes.js
const notes=require('./routes/api/notes')
//把数据库连接地址导入进来
const db=require('./config/keys').mongoURI
//连接mongodb数据库
mongoose.connect(db,{ useNewUrlParser: true, useUnifiedTopology: true })
.then(()=>console.log('MongoDB Connected'))
.catch(err=>console.log(err))
//使用body-parser中间件
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
//初始化passport
app.use(passport.initialize())
//引入passport.js并把passport传进去(所有的配置在passport.js文件中写,passport.js导出一个function,可以在这里传参)
require('./config/passport')(passport)
//使用routes
app.use('/api/users',users)
app.use('/api/profiles',profiles)
app.use('/api/notes',notes)
//设置端口号
const port=process.env.PORT || 5000
app.listen(port,()=>{
console.log(`Server running on port ${port}`)
})