Create REST API in Node.js
Today we will show you how to create REST API in Node.js. So we can use it in front-end application like ReactJS, Angular, Vue.js, etc.
Create REST API in Node.js, Building a simple REST API with NodeJS and Express, Creating a Secure REST API in Node.js. Building a Restful CRUD API with Node.js, Node.js and Express Tutorial: Building and Securing RESTful API, Example/Demo of Node.js REST API.
Checkout more articles on React JS:
Steps to Create REST API in Node.js
- Setup environment
- Initialize the project
- Setup the server using express
- Create server and add routing HTTP request
- Output
1. Setup Environment
In order to create REST API, We need Node.js is installed in system. Run below command to check the Node.js is installed or not. You can also use this command to check the version of it.
1 | node -v OR node --version |
If you don’t have it installed then use below link to download & install Node.js in your system.
2. Initialize the Project
I presume that you already have your environment set up. Kindly follow the below steps to initialize the project.
Now let’s create project directory name as “first-node-app“
Jump into the newly created directory via terminal.
Create a package.json file via below command in terminal.
1 | npm init |
package.json is a file that gives the necessary information like project name, version, description, project’s dependencies, etc.
“npm init
” will prompt you to enter some information such as name, version, description, entry point, test command, git repository, keywords, author, license, etc. At last type yes and press enter to complete the creation process.
If you want to skip all these things and directly create a project then just run below command.
1 | npm init -y |
After completing all these things, you will see the package.json has been created in your project directory.
3. Setup the Server using express
To set up the server, We need express framework for Node.js. Run following command to install express in project.
1 | npm install express |
Next, We will install nodemon package to track the file changes. nodemon is a tool that helps to auto restart the application when any file changes detected in the project directory. Run following command to install nodemon.
1 | npm install nodemon --save-dev |
The “--save-dev
” in above command will helps to install package as development dependency.
On successful installation, your package.json file will be modified to have the two newly installed packages.
4. Create server and add routing HTTP request
Let’s create “server.js” file at root level and type/copy following code.
server.js
1 2 3 4 5 6 7 | var express = require('express'), app = express(), port = process.env.PORT || 4000; app.listen(port, () => { console.log('Server started on: ' + port); }); |
In order to serve the application through nodemon, You have to add below command in “scripts” attributes of package.json file.
1 | "serve": "nodemon server.js" |
Now your updated package.json file should look like below.
By executing below command, you can start the application server. It will serve the application on 4000 port (http://localhost:4000
).
1 | npm run serve |
You can’t see anything in browser on http://localhost:4000 link because we are not serving any data on root URL.
So let’s add the request handler to serve the data on root URL. Add below code in server.js file to make it work.
1 2 3 4 | // request handlers app.get('/', (req, res) => { res.send('Welcome to the first Node.js Tutorial! - Clue Mediator'); }); |
Your whole server.js file should look like below.
1 2 3 4 5 6 7 8 9 10 11 12 | var express = require('express'), app = express(), port = process.env.PORT || 4000; // request handlers app.get('/', (req, res) => { res.send('Welcome to the first Node.js Tutorial! - Clue Mediator'); }); app.listen(port, () => { console.log('Server started on: ' + port); }); |
That’s it!! Now check the URL (http://localhost:4000
) again and it will serve you the above mentioned text in browser.
Because of nodemon you don’t have to restart the application. It will auto restart the application once file changes are detected.
aaah am a new in this programing language i almost used php nw i wanna move on this react but each site gives me different ways that confucing me where to beggin
Yes, It’s very difficult to find the correct way to learn ReactJS. I would recommend you to start learning all the react article from ReactJS category and it would be good if you will start reading from oldest article.
Feel free to share your queries with us.
We are happy to help you!
Join us on social pages & Share with your friends. Happy Coding..!!
Hey Mohammed,
We have created the separate category ReactJS Tutorial where we are listing the react articles. You can read it step by step.
Let us know if you are finding any difficulties.
Thanks,
Clue Mediator