Simple RESTful API Server with Node Js, Express Js and MongoDB

NodeJs and mongoDB

Node js is one of the most popular language in few years. There are so many resources and library to make this language is easy to learn.

Requirement

If you don’t use yarn, you can try it by installing yarn. You will get a miracle from it hahaha…

npm install -g yarn

Code quality is matter for long term development. Its because the clean code can be easy and common for all developer, so the project can be develop easily hand by hand. Eslint will help us to keep some rules to guide to the best quality of code.

Sometimes we must restart our server after changing something in the code. We will not have time for doing this things repeatedly. Nodemon will help us to auto restart after changing.

Global installation for eslint and nodemon

npm install -g eslint nodemon

Run the MongoDB database services

mongod --auth

Follow this command your terminal to create the simple-api-dev database with user authentication.

mongo
use simple-api-dev
db.createUser({
user: "admin",
pwd: "password",
roles: [{ role: "userAdmin", db: "simple-api-dev"}]
})

Create new folder called simple-api, open terminal and open the simple-api using the terminal. Hold your terminal in the folder project, because there are many activity in there.

First initialization

yarn init

You can just press enter until the end or you can answer all the question.

name (simple-api):
version (1.0.0):
description: just a simple api
entry point (index.js):
repository url:
author: hengky.anang.wijaya@gmail.com
license (MIT):
private: false

The command will generate package.json like below.

Install dependencies express, mongoose, mongoose-paginate, cors, morgan, body-parser, helmet

yarn add express mongoose mongoose-paginate cors morgan body-parser helmet

We using Rails-like folder structure. Create all file like down below.

| -- simple-api
|-- node_modules
|-- model
|-- index.js
|-- Post.js
     |-- controller
|-- index.js
|-- PostController.js
     |-- router
|-- index.js
     |-- config
|-- index.js
|-- dev.js
|-- prod.js
     |-- index.js
|-- .eslint
|-- .gitignore
`-- package.json

Create index.js in the root folder project. This file is the core of the server. There are 4 basic middleware

  • helmet, protect the express app with various HTTP Header Protection
  • cors, filter and setting the allowed origin that can access the server
  • bodyParser, parsing body request to json
  • morgan, HTTP request logger

In the beginning before the server listen on the specific post, the server will try to connect to the database using mongoose library.

Open config folder and create index.js, dev.js and prod.js, give it a logic to choose the development config or production config.

Create variable config in dev.js. This config is basic for the minimal requirement

From the root folder, we open the model folder. We will create index.js and Post.js

Post model will help us to define the schema of the Post collection in database. Mongoose can inject with plugin like mongoose-paginate to make instant pagination function for Post model.

Back to the root folder again, and open the controller folder, create index.js and PostController.js

Create, Read, Update and Delete, people called CRUD. This is a basic function for backend. A few things will make a magic begin. :)

Almost done, open the route folder, create index.js and write the routes. Define CRUD Post route and connect it with the Controller.

The last things, we must add scripts in the package.json. This scripts help us to call directly a long command equal with dev. You just doing this to call the command

yarn dev

It’s done! Test your API using Postman or you can also documented and testing with swagger. Keep exploring!

yarn dev

If you have any question or correction please tell me! Hope you enjoy it! :)

Complete source code available here :