How to use ExpressJS In NodeJS

Express is a back end web application framework for Node.js. It is one of the standard frameworks which many developers use. To install it, we will be using NPM (Node Package Manager).

Step 1: Installing npm in your directory

To install npm in a project, head over to the terminal and change your current working directory to that project. Then, use the npm init command to initialize the package.json file.

cd {your-project-directory}
npm init

Step 2: Installing express

Now that we have npm installed in our project, we can install Express by using the npm install command in our terminal.

npm install express --save

Step 3: Making express ready for use

After installing express, we can start writing code on our server. We need to require an express module before using it, and we can do that by adding the code given below at the top of our server code.

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

Step 4: Using express to return JSON data

Now our express is completely ready to use. In the example given below, we are returning data in the following manner:

  • In the ‘/’ route, we are returning an object that contains a single key-value pair.
  • In the ‘/multiple’ route, we are returning an object that contains multiple key-value pairs.
  • In the ‘/array’ route, we are returning an array of objects, each having multiple key-value pairs.

Example: Implementation to show returning the JSON data in nodeJS.

index.js
// Requiring express in our server
const express = require('express');
const app = express();

// Defining get request at '/' route
app.get('/', function(req, res) {
  res.json({
    number: 1
  });
});

// Defining get request at '/multiple' route
app.get('/multiple', function(req, res) {
  res.json({
    number: 1,
    name: 'John',
    gender: 'male'
  });
});

// Defining get request at '/array' route
app.get('/array', function(req, res) {
  res.json([{
      number: 1,
      name: 'John',
      gender: 'male'
    },
    {
      number: 2,
      name: 'Ashley',
      gender: 'female'
    }
  ]);
});

// Setting the server to listen at port 3000
app.listen(3000, function(req, res) {
  console.log("Server is running at port 3000");
});

Step to Run Application: Run the application using the following command from the root directory of the project

node index.js

Output: Your project will be shown in the URL http://localhost:3000/

Go to http://localhost:3000/multiple, you will see the following output.

Go to http://localhost:3000/array, you will see the following output.

How to Return JSON using Node.js ?

Node.js is a powerful platform for building server-side applications and returning JSON data is a common task in web development, especially when building APIs. JSON stands for JavaScript Object Notation. It is one of the most widely used formats for exchanging information across applications. Node.js supports various frameworks that help to make the processes smoother. The following ways cover how to return JSON data in our application from Node.js. This article will guide you through the process of setting up a simple Node.js server that returns JSON data in response to client requests.

Table of Content

  • Using ExpressJS
  • Using HTTP interface

Similar Reads

Using ExpressJS

Express is a back end web application framework for Node.js. It is one of the standard frameworks which many developers use. To install it, we will be using NPM (Node Package Manager)....

Using HTTP interface

Although the first method is sufficient for most solutions, there is another method that uses HTTP interface by Node.js and returns JSON data. Node.js comes with an in-built HTTP module, so we won’t have to install it separately as we did for express....

Conclusion

Returning JSON data in a Node.js application is straightforward using the Express framework. By following this guide, you can set up a simple server that responds with JSON data and expand it to handle dynamic data and multiple routes. This forms the basis for building robust APIs and web services with Node.js....

Contact Us