Node js with Express and MongoDB tutorial

In this tutorial, we will learn how to setup Nodejs with express. Express is a robust, fast Nodejs framework that provides rich features to develop web and mobile applications.

Before we start, make sure Nodejs, npm and Mongodb are set up in your system. if not please follow the below tutorial to install the latest version of nodejs.

Install Latest version of Nodejs on Ubuntu 16.04 and 18.04

To install Latest MongoDB you can follow below tutorials

Install MongoDB on ubuntu 16.04 and 18.04

Initialize package.json

Before we start we need to create package.json which will manage all the application dependencies for our application. To do so create a new directory.

mkdir expresstutorial && cd expresstutorial

The above command will create a new directory and enter it. Now initialize the package.json file. To do so enter the following command.

npm init

The above command will ask you a few questions. Below is an example.

npm init

After answering all the questions. The above command will create package.json file.

Install Expressjs framework

To install the express framework enter the following command in your terminal.

npm install express --save

The above command will install the express js framework and when you open your package.json file you'll see Express framework is automatically updated in your package.json file.

Create index.js file

Next, Create index.js file in your current directory. index.js file will be responsible for bootstrap all your application files. enter the following command to create index.js file

touch index.js

Now copy and paste the following code to update your index.js file.

var express = require('express');
var app = express();
var port = 3000;

app.listen(port, function() {
  console.log('Hello World!');

To run the application enter the following command in your terminal

node index

The above command will run your application and print "Hello world!"


Install Nodemon

It will be very difficult to run each time node index whenever you change the code. To Monitor your code changes and restart your application automatically we will use nodemon. To install enter the following command.

npm install -g nodemon

Once you are done with installing of nodemon. Update your package.js file with the following code

"scripts": {
    "start": "nodemon index",
    "test": "echo \"Error: no test specified\" && exit 1"

Now enter the following command to run your node application

npm start

Now when we change or update the code the above command will automatically restart

Install and connect MongoDB

To connect with MongoDB we will need to install the MongoDB package. To install Enter the following command.

npm install mongodb

Once the installation is completed. Update the index.js file with the following code.

var express = require('express');
var db = require('mongodb').MongoClient;
var app = express();
var port = 3000;

// setting up mongodb
var url = 'mongodb://localhost:27017/blog';
db.connect(url, function(error, res) {
    if (error)
        console.log('database connection error.');

    console.log('database connected');

app.listen(port, function() {
  console.log('Server up and running at port '+port);

The above code will print the following response if mongodb is connected.

// response
[nodemon] starting `node index index.js`
Server up and running at port 3000
database connected