Setup MongoDB with Koa.js

Victor Leung
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'); 

Keep the story going. Sign up for an extra free read.

You've completed your member preview for this month, but when you sign up for a free Medium account, you get one more story.
Already have an account? Sign in

Victor Leung

Written by

I’m a keen traveler to see every country in the world, passionate about cutting edge technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade