November 2018
Beginner
132 pages
2h 57m
English
We will define our MongoDB connection in a ./db.js file. This will help keep our index.js file neat and allow us to keep our configurations modular.
To connect to our database, if you haven't already, create a db.js file in the root directory as given:
touch db.js
Then, insert the following into the file:
// ./db.jsconst mongoose = require('mongoose');module.exports = () => { mongoose.connect( 'mongodb://localhost:27017/koa-blog', { useNewUrlParser: true } ); const db = mongoose.connection; db.on('error', error => { throw new Error(`error connecting to db: ${error}`); }); db.once('open', () => console.log('database connected'));};
In the preceding code block, first, we require the mongoose ODM library, then we export ...
Read now
Unlock full access