To create an HTTP server in Node.js, you can use the built-in http
module. Here is an example:
index.tsx862 chars35 lines
This creates a simple HTTP server that listens on port 3000. When a POST request is received, it reads the request body as a string and parses it as JSON. If parsing succeeds, it sends a response containing the parsed data. If parsing fails, it sends an error response.
To test this, you can use a tool like Postman to send a POST request with a JSON body to http://localhost:3000
. The response should contain the message "Request received" and the data you sent.
gistlibby LogSnag