codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

21 / 76 topics
20Redirecting Requests in Express.js21Setting HTTP Status Codes22Sending JSON Responses
Tutorials/Express.js/Setting HTTP Status Codes
🚂Express.js

Setting HTTP Status Codes

Updated 2026-05-15
10 min read

Setting HTTP Status Codes

Introduction

In web development, HTTP status codes are essential for communicating the outcome of a client's request to the server. They provide information about whether the request was successful or if there were any issues that needed addressing. In this tutorial, we will explore how to set appropriate HTTP status codes in responses using Express.js.

Concept

HTTP status codes are standardized three-digit numbers that indicate the result of an HTTP request. They are grouped into five classes:

  1. Informational responses (100–199): The request was received, continuing process.
  2. Successful responses (200–299): The request was successfully received, understood, and accepted.
  3. Redirection messages (300–399): Further action needs to be taken in order to complete the request.
  4. Client error responses (400–499): The request contains bad syntax or cannot be fulfilled.
  5. Server error responses (500–599): The server failed to fulfill an apparently valid request.

Understanding these codes is crucial for building robust and user-friendly APIs. Express.js makes it straightforward to set these status codes in your response objects.

Examples

Basic Usage

Let's start with a simple example where we send a 200 OK response:

JavaScript
1const express = require('express');
2const app = express();
3const PORT = 3000;
4
5app.get('/', (req, res) => {
6res.status(200).send('Hello World!');
7});
8
9app.listen(PORT, () => {
10console.log(`Server is running on http://localhost:${PORT}`);
11});

In this example, we use res.status(200) to set the HTTP status code to 200 before sending a response.

Handling Errors

When handling errors, it's important to send appropriate error status codes. For instance, if a resource is not found, you should return a 404 Not Found status:

JavaScript
1app.get('/user/:id', (req, res) => {
2const userId = req.params.id;
3
4// Simulate user lookup
5if (userId === '1') {
6 res.status(200).json({ id: 1, name: 'John Doe' });
7} else {
8 res.status(404).send('User not found');
9}
10});

Here, we check if the user ID exists and respond accordingly. If the user is not found, we send a 404 status code.

Using Status Codes in JSON Responses

When sending JSON responses, you can still set the HTTP status code:

JavaScript
1app.get('/api/data', (req, res) => {
2const data = { message: 'Data fetched successfully' };
3
4// Send a 201 Created status code
5res.status(201).json(data);
6});

In this example, we send a JSON response with a 201 Created status code.

What's Next?

Now that you understand how to set HTTP status codes in Express.js, the next step is to learn about sending JSON responses. This will allow you to build more sophisticated APIs that can handle complex data structures and communicate effectively with clients.

Stay tuned for the next tutorial on "Sending JSON Responses"!

Info

Remember, using appropriate HTTP status codes enhances your API's usability and helps developers understand the outcome of their requests.


PreviousRedirecting Requests in Express.jsNext Sending JSON Responses

Recommended Gear

Redirecting Requests in Express.jsSending JSON Responses