To return a JSON response from a HTTP server in Node.js, you can use the built-in http
module and the JSON.stringify()
method.
Here's an example:
index.tsx299 chars12 lines
In this example, we create a server using the http.createServer()
method. When a request is made to this server, we create a JSON object data
, set the content type of the response to application/json
, and use JSON.stringify()
to convert data
to a JSON string. Finally, we send the JSON string as the response using res.end()
.
gistlibby LogSnag