Create Reuseable CodeBase Rest API NodeJS, Express App

Want to create a mobile app? Or want to develop a Web App? Then you’ll need to create a Rest API. This article shows you how to create a Reuseable CodeBase Rest API Node.js. If you are planning to lower your expenses and raise your revenues, you will first need to grow your business. This will make you look for ways to improve your business and grow revenues. This article explains how to build a rest API in NodeJS and create a codebase that can be reused in your future projects.

Preparation For Creating NodeJs, Express App

  • Install NodeJs

Node.js is an open source, cross-platform runtime environment for developing server-side applications. It is most commonly used by JavaScript developers, and its asynchronous, event-driven architecture makes it ideal for data-intensive real-time applications that run across distributed devices. In this article we will highlight the prerequisites for creating the NodeJS application; including the tools, the environment and where to get started. if you not install it yet please visit the NodeJs Official site to get started to install the Node.

In this step, you’ll create a Postman collection to make it easier to check the API that will be created later. I think Postman is more convenient to use because it supports multiple tabs. Make sure the application is installed properly by opening the application.

Creating a CRUD API With Node.js and Express

now if you already install all above preparation before we start creating rest api using nodejs lets get going to another step :

Creating New Project

We are going to create a new project in any directory we want. This project will be named ctrlsal-nodejs-api. Once this directory has been created, open Terminal or git bash in that directory and type npm init.

Keep going until you finish the configuration process. A file named package.json will be automatically created in the ctrlsal-nodejs-api directory. After that we need to check if the Project is working properly with creating server.js in the main directory, and look like this :

Setting Package.json nodejs

the server.js is look like this :

then we can run npm run server to start the program in terminal

Check NodeJs Working Properly

if you see the hello world then the server.js and your nodejs Installation is working properly, so we can go to the next step.

Library installation is required


Express is one of the most popular Node frameworks, and its installation process is straightforward. To install Express from the ctrlsal-nodejs-api directory run: npm i express –save . If installation was successful in this example these messages will appear :

Installing ExpressJs on Nodejs Directory

in another way we wont to start again, again, and again the command “npm run server” so we need to add some library that called “nodemon” and “concurrently”. you just need to copy this code snippet below, and paste it in your console.

npm i --save -D nodemon concurrently

after that you will see the package.json file change and add devDependencies json look like in this screenshot below :

Installing Nodemon and ConCurrently

now we need to modified the “scripts” bracket with this code :

  "scripts": {
    "server": "nodemon backend/server",
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "concurrently \"npm run server\""

and then run the application with npm run dev instead of npm run server. When the application is started, the earliest code to be executed is in the server.js file. Empty the file and add the following code :

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

app.get('/helloworld', (req, res) => {
  res.send('Hello World!')

app.listen(port, () => {
  console.log(`ctrlsal-nodejs-api listening at http://localhost:${port}`)

First, import from the Express library with const express = require(‘express’);. Then initialize the framework in const app = express();.

Next, we create a new route at ‘/helloworld’ and then add it to the app.get() function while passing a callback function that will be executed when this endpoint is called. The callback should contain the line “res.send()” which returns information based on the request or activity by another party.

Finally, the app.listen(port) is fired up and checks for client connections all the time. Run “npm run dev” in the root directory of ctrlsal-nodejs-api. If it is in accordance with the image below, it means that the application has been successfully run.

Running Server.js in Git Bash

Go to Next Part :

  • Create Reuseable CodeBase Rest API NodeJS, Express App Part 2

Leave a Comment