连接本地MongoDB:
// 1.安装mongoose模块
npm install mongoose --save-dev
// 2.在app.js中引入mongoose 模块
var mongoose = require('mongoose')
// 3.连接mongodb数据库
var mongoDB = 'mongodb://127.0.0.1:27017/express-library';
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
发表回复