Complete Node js Project Setup from Docker to Testing-[Docker |RESTfull APIs with Node.js | Express.js | MongoDB | Integration Testing with JEST]

In this tutorial, I’ll show you how to set up a simple project with Node.js + Express.js + MongoDB + JEST Integration Testing and how to dockerize the project.

Prerequisites: basics of node js, express, MongoDB, integration testing, and docker.

Before going to work with docker I want to set up a basic project with express.js and node.js. Create/Select a folder for our project inter that project through your terminal and run :

$ npm init

Now it will ask you some questions about package name, version, entry point etc. Hit enter if you want to keep the default one. After that, you will get something like that

press ‘yes’ and you are ready to go. It creates a file name package.json.

Now I would like to add some dependencies :

$ npm i express mongoose passport passport-jwt jsonwebtoken body-parser bcryptjs validation

copy the whole command or type it and hit the enter button and you will see something like this :

express: which is our main framework.

mongoose: which is used to connect/interact with MongoDB.

passport: which is used for authentication.

jsonwebtoken: is used for generating the token.

body-parser: by using this we can get the data throughout the request.

validator: is used for validation.

Now I want to add a dev-dependency ( nodemon ). If you don’t want to add then you can skip this, it’s optional.

$ npm i -D nodemon

Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.

At that point, your package.json should look like that


Now create a file name app.js ( entry point ). You can create this from the project folder by the command below ( it works for mac)

$ touch app.js

paste the code below,

Now, run the command

$ node app

you will see, Server running on port 8082. you can also check it from the browser. Open the browser and hit at http://localhost:8082

now, if we change anything then we need to restart the server manually. but if we set the nodemon then we don’t have to restart it every time. Nodemon will watch if there is any change then it will restart automatically. so what you need to do for that is a little change in the package.json file ( change the scripts ). see the package.json file below

So, now you can run your project using this command

$ npm run app

Let’s complete our database setup. I’ll create a mongo instance from the docker a bit later but I want to complete the setup code for database connection before going to the docker-compose.

create a folder named config and add three files with the corresponding code mentioned above. keys_dev.js exports the database for the development purpose and the keys_test.js exports the database for the testing purpose and keys.js decides which database to export when. Now update your app.js file

So far so good. now, we are ready to go for the docker setup. Create three files ( Dockerfile, docker-compose.yml and .dockerignore ) in the root folder with the code mentioned below

now you can run your project using the command

$ docker-compose up

also you can access mongo and app instances using these command

// app
$ docker-compose exec app bash
// mongo
$ docker-compose exec mongo bash

If you want to remove the docker container then use

$ docker-compose down

Now, time to complete route setup. we will see how to create user registration, login and logout APIs. So, let’s create a folder named routes. Inside the routes folder create another folder named api, which is going to hold all your APIs. So create a file named user.js inside the api folder with the code below

To access this file from the app.js (root point) we need to update our app.js file with the code below

// initializing routes
const users = require('./routes/api/users');
// use Routes
app.use('/api/users', users);

and your app.js file should be like this

Now we can access our users route through http://localhost/api/users/test

Before going to register user we need to create user model so that we can store user information to the database. Let’s create a folder named models and inside it create another file named User.js with the code below

Let’s create a register route with proper logical code.

also, need to update our app.js ( root pointer ) file with the code below

const bodyParser = require('body-parser');
const app = express();
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));

NOTE: and the rest will be published soon …

See the full source code here.

Software Engineer at Pathao Ltd. | Sport Programmer | |

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