Setup MongoDB with Koa.js
Nov 3 · 2 min read
I’m building a Koa.js server and need to connect to MongoDB to store and retrieve data. Here is how to do it with some simple steps:
Step 1: Connect the database before Koa app initialised
const initDB = require('./database'); initDB(); const app = new Koa();
Inside the database.js, import mongoose. You will need to npm install -save mongoose as well. It is an Object Data Modeling (ODM) library.
const mongoose = require('mongoose'); 
