Create REST API in Node.js

Ankit Kumar Rajpoot
3 min readSep 7, 2020

--

Rest Apis Nodejs

Representational state transfer (REST) is an architectural design pattern for APIs. APIs that follow this pattern are called REST APIs or RESTful APIs. REST sets certain standards between computer systems on the web that make it easier for systems to communicate with each other.

In this article, We learning that hoe to create REST Apis in NodeJs and use it. In fact, we will set up a complete project through an npm module.

There are some basic steps

  1. Setup environment
  2. Initialize the project
  3. Change directory
  4. Install dependencies
  5. Start project

Setup environment:-

Firstly, We check there are all modules are installed in our local machine or not. There are some required modules and some optional. Node is required and npm, nvm are optional. I am supposing that node is available in your machine if it’s not then follow. https://blog.teamtreehouse.com/install-node-js-npm-mac

You can check the node is available or not in your machine through the following command.

$ node -v
node version

Initialize the project OR Generate project:-

You can initialize the project from scratch via npm init command. But there is another way, you can install express-generator the module and create a pre-structured project or boilerplate that will help you to forward your project without any interruptions.

$ sudo npm install express-generator -g
install express-generator

After installing express-generator , We will create a node project through the following command.

$ express first-node-app
create a project

Change directory:-

After creating first-node-ap , We enter the directory through the following command.

$ cd first-node-app
change directory
project structure

Install dependencies:-

After Entering the directory, We install dependencies through the following command.

$ npm install
install dependencies

After installing dependencies there will be one extra folder, which is node_modules . You can see the given image.

structure

Start Project:-

After completing everything we start the project through the following command.

$ npm start
start project

After starting the project, You will have to search http//:localhost:3000 you will find the following page.

first route
second route

In this project, two routes are already available. Now you can add your own routes and forward this project according to you.

That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!!

--

--

Ankit Kumar Rajpoot
Ankit Kumar Rajpoot

Written by Ankit Kumar Rajpoot

I’m a MERN Developer. ( Redux | AWS | Python ) I enjoy taking on new things, building skills, and sharing what I’ve learned.

No responses yet