mongoose连接mongodb数据库教程

2018.07.30 11:19 阅读 23

首先在项目中安装mongoose依赖

  npm install mongoose -S
const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/blog')
const db = mongoose.connection
db.on('error', function() {
  console.log('数据库连接失败:' + error)
})
db.on('open', function() {
  console.log('数据库连接成功')
})