Create REST API in Node.js
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
- Setup environment
- Initialize the project
- Change directory
- Install dependencies
- 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
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
After installing express-generator
, We will create a node project through the following command.
$ express first-node-app
Change directory:-
After creating first-node-ap
, We enter the directory through the following command.
$ cd first-node-app
Install dependencies:-
After Entering the directory, We install dependencies through the following command.
$ npm install
After installing dependencies there will be one extra folder, which is node_modules
. You can see the given image.
Start Project:-
After completing everything we start the project through the following command.
$ npm start
After starting the project, You will have to search http//:localhost:3000
you will find the following page.
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 !!!!!!