Open In App

Express: Specify HTTP status code when throwing error in service

Improve
Improve
Like Article
Like
Save
Share
Report

In express, we can use res.status(–statusCode–) to set the status code. Let’s create a simple web application using express and create a blog route, and when the user tries to access that route it will send the status code.

Syntax:

res.status(--statusCode--);

where you have to change the –statusCode– to the code you want to set.

Creating Application:

Step 1: Initializes NPM: Create and Locate your project folder in the terminal & type the command

npm init -y

It initializes our node application & makes a package.json file.

Step 2: Install Dependencies: Locate your root project directory into the terminal and type the command

npm install express

To install Express as dependencies inside your project

Step 3: Creating Routes for the home page and the blog page: Let’s create two routes so that users can access the home page and the blog page.

app.get('/', (req, res) => {
   res.send('Hello Geeks!');
});

app.get('/blog', (req, res) => {
   res.status(400);
   res.send('Error');
});

Inside blog routes we use res.status function to set the status code to 400 which throws when the user requests for the blog page. 

Example 1:

Javascript




const express = require('express');
const app = express();
  
app.get('/', (req, res) => {
    res.send('Hello Geeks!');
});
  
app.get('/blog', (req, res) => {
    res.status(400);
    res.send('Error');
});
  
app.listen(3000);


Steps to run the application: Inside the terminal type the command to run your script.

node app.js

Output:

 

Creating Application:

Step 1: Initializes NPM: Create and Locate your project folder in the terminal & type the command

npm init -y

It initializes our node application & makes a package.json file.

Step 2: Install Dependencies: Locate your root project directory into the terminal and type the command

npm install express

To install Express as dependencies inside your project

Step 3: Creating list of products: Let’s create an array of products and set it to a constant products.

const products = ["Milk", "Sugar"];

Step 4: Creating Routes for the home page and the products page: Let’s create two routes so that users can access the home page and the products page.

app.get('/', (req, res) => {
    res.send('Hello Geeks!');
});

app.get('/products', (req, res) => {
    if (products.length === 0) {
        res.status(400);
        res.send('No products found!');
    } else {
        res.json(products);
    }
});

Inside the product route, we use res.status function to set the status code to 400 which throws when there is no product. If there are products it will send the list as JSON.

Example 2:

Javascript




const express = require('express');
const app = express();
const products = ["Milk", "Sugar"];
  
app.get('/', (req, res) => {
    res.send('Hello Geeks!');
});
  
app.get('/products', (req, res) => {
    if (products.length === 0) {
        res.status(400);
        res.send('No products found!');
    } else {
        res.json(products);
    }
});
  
app.listen(3000);


Steps to run the application: Inside the terminal type the command to run your script.

node app.js

Output:

 



Last Updated : 11 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads