See associated video.
$ git clone https://gist.github.com/98a60b5ee632b94e6353af3b632246d3.git mongoose
$ cd mongoose
$ npm installFirst start mongod:
$ mongodThen connect to database:
$ node connection| var mongoose = require('mongoose'); | |
| mongoose.connect('mongodb://localhost/dbname'); | |
| var connection = mongoose.connection; | |
| connection.on('connected', function() { | |
| console.log('connected to db'); | |
| }); | |
| connection.on('disconnected', function() { | |
| console.log('disconnected to db'); | |
| }); | |
| connection.on('error', function(error) { | |
| console.log('db connection error', error); | |
| }); | |
| process.on('SIGINT', function() { | |
| connection.close(function() { | |
| console.log('db connection closed due to process termination'); | |
| process.exit(0); | |
| }); | |
| }); | |
| module.exports = connection; |
| { | |
| "dependencies": { | |
| "mongoose": "^4.7.6" | |
| } | |
| } |