Express mongodb

作者: Tiny_z | 来源:发表于2018-03-21 23:33 被阅读26次

安装express

npm install express --save
const express = require('express')
const app = express()

app.get('/',function(req,res){
   res.send('<h1>Hello React</h1>')
})

app.get('/data',function(req,res){
   res.json({name:zc})
})

app.listen(9093,function(){
    console.log('Node app start at port 9093')
})

监听路由和响应内容,使用nodemon自动重启

npm install nodemon -g

执行 nodemon file.js

安装 mongodb

brew install mongodb

启动mongodb
1.mongod --config /usr/local/etc/mongod.conf

安装mongoose node和mongodb连接的一个库

npm install mongoose --save
const DB_URL = 'mongodb://localhost:27017'
mongoose.connect(DB_URL);
mongoose.connection.on('connected',function(){
  console.log('mongo connect success')
})

通过mongoose来操作mongodb,存储的就是json

相关文章

网友评论

    本文标题:Express mongodb

    本文链接:https://www.haomeiwen.com/subject/ycxoqftx.html