-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
59 lines (45 loc) · 1.54 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
// 引入express模块
var express = require('express')
// 创建app对象
var app = new express()
// 引入 api 路由
var router = require('./route/index')
/* 使用mongoose类库 */
var mongoose = require('mongoose')
/* 链接 vuechick 数据库 */
var url = "mongodb://127.0.0.1:27017/vuechick"
/* 链接 */
mongoose.connect(url)
/* 链接成功 */
mongoose.connection.on('connected',()=>{
console.log('连接成功')
})
// 链接异常
mongoose.connection.on('error',()=>{
console.log('error')
})
// 链接断开
mongoose.connection.on('disconnected',()=>{
console.log('断开连接')
})
const bodyParser = require('body-parser')
app.use(bodyParser.json())
//这个要放在所有的use前面,解析我们的post请求数据
app.use(bodyParser.urlencoded({ extended: true}))
// 解决跨域
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
next();
});
// 使用
app.get('/',function(req,res){ // 这个 /表示当前根目录 访问的时候直接 localhost:3000
res.end("hello world !");
})
app.use('/api', router) // 将路由注册到/api的路径下
var port = process.env.PORT || 3000
// 定义服务器启动端口
app.listen(port)
console.log('app is listening on port: ' + port)