Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Express res.sendStatus() Method

Updated on Feb 18, 2024
By Mari Selvan
👁️ 36 - Views
⏳ 4 mins
💬 1 Comment
Express res.sendStatus() Method

Photo Credit to CodeToFun

🙋 Introduction

When building web applications with Express.js, sending appropriate HTTP status codes is crucial for conveying the outcome of a request.

The res.sendStatus() method simplifies this process by allowing you to send a specific status code along with an optional status message.

In this guide, we'll explore the syntax, use cases, and best practices of the res.sendStatus() method in Express.js.

💡 Syntax

The syntax for the res.sendStatus() method is straightforward:

syntax.js
Copied
Copy To Clipboard
res.sendStatus(statusCode)
  • statusCode: A numeric HTTP status code to be sent in the response.

❓ How res.sendStatus() Works

res.sendStatus() sets the response status code and sends its string representation as the response body. It's a convenient method when you want to send a minimal response with just the status code and a default status message.

example.js
Copied
Copy To Clipboard
app.get('/success', (req, res) => {
  // Send a 200 OK status with default message "OK"
  res.sendStatus(200);
});

app.get('/notfound', (req, res) => {
  // Send a 404 Not Found status with default message "Not Found"
  res.sendStatus(404);
});

In this example, different routes demonstrate how to use res.sendStatus() with specific status codes.

📚 Use Cases

  1. Simplifying Success Responses:

    Use res.sendStatus() to quickly send success responses without specifying a custom message.

    example.js
    Copied
    Copy To Clipboard
    app.post('/submit', (req, res) => {
      // Process form submission
      // If successful, send a 200 OK status
      res.sendStatus(200);
    });
  2. Handling Errors:

    Easily handle error scenarios by sending appropriate status codes with res.sendStatus().

    example.js
    Copied
    Copy To Clipboard
    app.get('/resource/:id', (req, res) => {
      const resourceId = req.params.id;
      // Query the database for the resource
      // If not found, send a 404 Not Found status
      if (!resourceExists(resourceId)) {
        res.sendStatus(404);
      } else {
        // Send the resource data
        res.json(getResourceData(resourceId));
      }
    });

🏆 Best Practices

  1. Be Explicit in Code:

    When using res.sendStatus(), be explicit about the status codes you're sending to ensure clarity in your code.

    example.js
    Copied
    Copy To Clipboard
    // Prefer this
    res.sendStatus(404);
    
    // Over this
    res.status(404).send('Not Found');
  2. Leverage Default Messages:

    Take advantage of the default status messages provided by res.sendStatus() to keep your responses concise.

    example.js
    Copied
    Copy To Clipboard
    app.post('/submit', (req, res) => {
      // Process form submission
      // If successful, send a 200 OK status with default message "OK"
      res.sendStatus(200);
    });

🎉 Conclusion

The res.sendStatus() method in Express.js streamlines the process of sending HTTP status codes and default messages. Whether you're handling success responses or managing errors, incorporating res.sendStatus() can lead to cleaner and more maintainable code.

Now, armed with knowledge about the res.sendStatus() method, simplify your response handling in Express.js projects and ensure effective communication with clients.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
2 months ago

If you have any doubts regarding this article (Express res.sendStatus() Method), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy