← Back to Node.js Course | Chapter 6: HTTP Module | Lesson 7 of 7

Status codes

Status codes are three-digit numbers that tell the client whether a request worked.

In this page:

  1. Status codes
Syntax
javascript
res.status(statusCode).json(body);
res.sendStatus(statusCode);

Status codes

2xx means success (200 OK, 201 Created, 204 No Content), 3xx redirects, 4xx client mistakes (400, 401, 403, 404) and 5xx server faults (500, 503).

Choosing accurate codes helps clients and tools react correctly. http.STATUS_CODES maps numbers to names.

Note: http.STATUS_CODES[404] gives Not Found.

Example: Status codes

javascript
const http = require("http");
for (const code of [200, 201, 204, 301, 400, 401, 403, 404, 500]) {
  console.log(code, http.STATUS_CODES[code]);
}

// Output:
// 200 OK
// 201 Created
// 204 No Content
// 301 Moved Permanently
// 400 Bad Request
// 401 Unauthorized
// 403 Forbidden
// 404 Not Found
// 500 Internal Server Error

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Returning 200 for errors
  2. Confusing 401 and 403
  3. Using 500 for client mistakes
Chapter Summary
  • 2xx success, 3xx redirect
  • 4xx client error, 5xx server error
  • 201 for created, 204 for empty
  • Use accurate codes
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.