Here's an example of how to create an HTTP server in TypeScript, parse the request body as JSON, and return a JSON response:
index.ts775 chars32 lines
This code creates an HTTP server that listens on port 3000. When a POST request is made with the Content-Type
header set to application/json
, the request body is parsed as JSON and a response is returned with a message of "Success!" and the parsed JSON data. If the request body is not valid JSON, a 400 error response is returned. If the request method is not POST or the Content-Type
header is not set to application/json
, a 404 error response is returned.
gistlibby LogSnag