Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
send(), sendStatus() and json() method in Node.js
In Express.js, the send(), sendStatus(), and json() methods are essential for sending responses to clients. The send() method sends data in string format, json() sends data in JSON format with proper headers, and sendStatus() sends HTTP status codes with default status messages.
Prerequisites
Node.js installed
Basic understanding of Express.js
Installation
Install the Express module using npm:
npm install express
Understanding sendStatus() Method
The sendStatus() method sends an HTTP status code along with its default message. Common status codes include 200 (OK), 404 (Not Found), 201 (Created), and 500 (Internal Server Error).
// Importing the express module
const express = require('express');
const app = express();
// Sending status response for '/' path
app.get('/', (req, res) => {
// Status: 200 (OK)
res.sendStatus(200);
});
// Setting up the server at port 3000
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Understanding send() Method
The send() method sends a response to the client. It can handle strings, HTML, buffers, and objects. Express automatically sets the appropriate Content-Type header.
// Importing the express module
const express = require('express');
const app = express();
// Initializing the heading string
var heading = "Welcome to TutorialsPoint!";
// Sending response for '/' path
app.get('/', (req, res) => {
// Sending the heading text
res.send(heading);
});
// Setting up the server at port 3000
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Understanding json() Method
The json() method sends a JSON response with the appropriate Content-Type header set to "application/json". It's specifically designed for API responses.
// Importing the express module
const express = require('express');
const app = express();
// Initializing data as JSON object
var data = {
portal: "TutorialsPoint",
tagLine: "SIMPLY LEARNING",
location: "Hyderabad"
};
// Sending JSON response for '/' path
app.get('/', (req, res) => {
// Sending the data as JSON
res.json(data);
});
// Setting up the server at port 3000
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Comparison of Methods
| Method | Purpose | Content-Type | Use Case |
|---|---|---|---|
send() |
General response | Auto-detected | Text, HTML, or mixed content |
json() |
JSON response | application/json | API endpoints |
sendStatus() |
Status with message | text/plain | Quick status responses |
Key Differences
send(): Versatile method that can send various data types and automatically sets Content-Type
json(): Specifically for JSON responses, sets proper headers and formats data
sendStatus(): Combines status code with default message, ends the response immediately
Conclusion
Choose json() for API responses, send() for general content, and sendStatus() for quick status responses. Each method serves specific use cases in Express.js applications and helps maintain proper HTTP communication standards.
