Node.js Building simple REST API in express Last Updated : 14 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Let's have a brief introduction about the Express framework before starting the code section:Express: It is an open-source NodeJs web application framework designed to develop websites, web applications, and APIs in a pretty easier way. Express helps us to handle different HTTP requests at specific routes.As it is NodeJs web framework so make sure that NodeJs has been installed on our system. For verifying type the following command in the terminal: node -v It will show the installed version of NodeJs to our system as shown in the below screenshot. STEP-1: Create a separate folder and with the help of a terminal or command prompt navigate to this folder: STEP-2: Create package.json by typing the following command in the terminal: npm init -y For knowing more about package.json click here. STEP-3: Create a file named server.js at the root of the project. Now, our folder structure will be as shown in the screenshot below: javascript // server.js File const express = require('express'); // Importing express module const app = express(); // Creating an express object const port = 8000; // Setting an port for this application // Starting server using listen function app.listen(port, function (err) { if(err){ console.log("Error while starting server"); } else{ console.log("Server has been started at "+port); } }) STEP-4: Start server by typing the following command in the terminal: node server.js STEP-5: Open the browser and type http://localhost:8000 and we will get the following response. We are getting Cannot GET / response as we are trying to access/route to the server and there is nothing mounted to that particular route. STEP-6: Handing the route to the server Handle the root route of the server by sending something back to it and adding the following code to the server.js app.get('/', function (req, res) { res.send('we are at the root route of our server'); }) Now, restart the server by typing the following command : node server.js Comment More infoAdvertise with us Next Article Node.js Building simple REST API in express A AshishkrGoyal Follow Improve Article Tags : JavaScript Web Technologies Node.js NodeJS-Questions Similar Reads How to use TypeScript to build Node.js API with Express ? TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will 4 min read Using Restify to Create a Simple API in Node.js Restify is an npm package that is used to create efficient and scalable RESTful APIs in Nodejs. The process of creating APIs using Restify is super simple. Building a RESTful API is a common requirement for many web applications. Restify is a popular Node.js framework that makes it easy to create RE 6 min read How to Build a RESTful API Using Node, Express, and MongoDB ? This article guides developers through the process of creating a RESTful API using Node.js, Express.js, and MongoDB. It covers setting up the environment, defining routes, implementing CRUD operations, and integrating with MongoDB for data storage, providing a comprehensive introduction to building 6 min read Creating a REST API Backend using Node.js, Express and Postgres Creating a REST API backend with Node.js, Express, and PostgreSQL offers a powerful, scalable solution for server-side development. It enables efficient data management and seamless integration with modern web applications.This backend can do Query operations on the PostgreSQL database and provide t 4 min read Explain the use of req and res objects in Express JS Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send 4 min read Like