Mongoose 连接

一定要先运行 mongodb! mongod --dbpath data/

package.json

"dependencies": {
    "mongoose": "^4.5.5",
}

server.js(ECMA 6)

import mongoose from 'mongoose';

mongoose.connect('mongodb://localhost:27017/stackoverflow-example');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error!'));

server.js(ECMA 5.1)

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/stackoverflow-example');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error!'));