How To Build A REST API With Node.js; Express.js and the MongoDB Driver

Okay: so you want to build a REST API with Node.js; Express.js and MongoDB. Now that you are on the article: let’s get started!

First you want to start off the project in a new directory. Initiate the app with the Node Package Manager to create a new Node.js project.

npm init -y

The “-y” expedites the process and allows for the package.json file to built without the command prompt. Next one wants to create the server file.

touch index.js

Lastly in the setup of the new Node.js project one wants to install the necessary dependencies.

npm i mongodb express

Notice for this tutorial I am using the mongodb driver. For this tutorial I am also using the express module for http requests. Since I am on a Mac I will be installing and running MongoDB locally. This is done by installing the xcode tools and then installing MongoDB with this command.

brew tap mongodb/homebrew

Then one can start MongoDB with this command:

brew services start mongodb-community

Note: if you are on a Windows or want to use the cloud string associated with your MongoDB account: go ahead. This can be done by signing up for a MongoDB account at https://account.mongodb.com/account. From there you want to create a cluster; whitelist your IP Address and then use the cloud string from your account instead of the local string I will provide here. From here I am going to build out the web server with Express. The index.js file looks as follows.

// Require Dependencies
let MongoClient = require('mongodb').MongoClient
let express = require('express')
// Define MongoDB Variables
let dbName = "clients"
let url = "mongodb://localhost:27017"
let db
// Connect to MongoDB
MongoClient.connect(url, function(err, client) {
if (err) { console.log(err)} else { db = client.db(dbName)})

As you can see here I defined the necessary variables; connected to the database and defined the db instance. Now we can use the db variable to access the data. From here I build the Express portion of the index.js file.

// Define express's app
let app = express()
// Start server
app.listen(8080)

This may work just fine in the browser but one is to observe there is no routes! From here we build our express routes.

app.get('/api/clients', async (req, res) => {     await db.clients.find({}, function(err, Clients) {           if (err) {
res.json(err)
} else {
res.json(Clients)
}
})})

And there you have it! This was the building of a REST API with the MongoDB Driver! Some steps from here could include adding a POST; PUT and DELETE routes. The documentation for such can be found here: https://mongodb.github.io/node-mongodb-native/api-generated/collection.html. Best of luck on your REST API adventures!

Reference

MongoDB. (n.d.). Install MongoDB Community Edition. Retrieved from https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/.

MongoDB. (2020). MongoDB Driver. Retrieved from https://www.npmjs.com/package/mongodb.

These articles pertain to Full Stack Development. Find Angular 2+ book here: https://www.lulu.com/account/projects/7dqz7r.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store