Facebook Pixel

How do I send JSON from a Node.js server?

Call response.writeHead(200, { 'Content-Type': 'application/json' }), then response.end(JSON.stringify(data)). Set the Content-Type to application/json and send a JSON string.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How to Create an HTTP Server in Node.js From Scratch

Import the http module with require('http'), call http.createServer with a request listener function, and listen on a port with server.listen(port). The listener receives request and response objects.

A function that receives request and response. The request has the URL, method, and headers. The response is what you send back: status code, headers, and body via writeHead and end.

Start with the http module to understand how servers work, then use Express for productivity. Express handles routing, middleware, and request parsing, but understanding the raw module first makes Express more meaningful.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.