How to Send JSON Response in Node.js: Simple Guide
In Node.js, you can send a JSON response by setting the
Content-Type header to application/json and sending a JSON string using res.end() with the built-in http module. When using Express, simply use res.json() to send a JSON response easily.Syntax
To send a JSON response in Node.js, you need to set the Content-Type header to application/json and send the JSON data as a string. In the built-in http module, use res.writeHead() to set headers and res.end() to send the response. In Express, use res.json() which handles headers and stringifying automatically.
javascript
/* Using built-in http module */ res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ key: 'value' })); /* Using Express framework */ res.json({ key: 'value' });
Example
This example shows a simple Node.js server using the built-in http module that sends a JSON response. It also shows how to do the same with Express for easier handling.
javascript
/* Using built-in http module */ const http = require('http'); const server = http.createServer((req, res) => { if (req.url === '/data') { const data = { message: 'Hello, JSON!' }; res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(data)); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); } }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); }); /* Using Express framework */ const express = require('express'); const app = express(); app.get('/data', (req, res) => { res.json({ message: 'Hello, JSON!' }); }); app.listen(3000, () => { console.log('Express server running at http://localhost:3000/'); });
Output
Server running at http://localhost:3000/
Express server running at http://localhost:3000/
Common Pitfalls
- Forgetting to set the
Content-Typeheader toapplication/jsoncan cause clients to misinterpret the response. - Sending a JavaScript object directly without converting it to a JSON string using
JSON.stringify()will cause errors in the built-inhttpmodule. - Using
res.send()in Express without JSON data may send plain text instead of JSON.
javascript
/* Wrong way with http module (missing JSON.stringify) */ res.writeHead(200, { 'Content-Type': 'application/json' }); res.end({ message: 'Hello' }); // This will cause an error /* Right way */ res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: 'Hello' }));
Quick Reference
| Method | Usage | Notes |
|---|---|---|
| http module | res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(data)); | Manual header and string conversion required |
| Express | res.json(data); | Automatically sets header and stringifies object |
| Common mistake | res.end(object); | Causes error, must stringify first |
Key Takeaways
Always set the Content-Type header to application/json when sending JSON responses.
Use JSON.stringify() to convert objects to JSON strings when using the built-in http module.
Express's res.json() method simplifies sending JSON by handling headers and stringifying automatically.
Avoid sending raw objects directly in res.end() as it causes errors.
Test your API responses in a browser or tool like Postman to verify JSON format.