You can use the built-in http
module in Node.js to create an HTTP server and receive requests. To parse the request body as form-urlencoded, you can use the querystring
module.
Here's an example code snippet in TypeScript:
index.ts565 chars24 lines
In this example, we create an HTTP server that listens on port 8080. When the server receives a POST
request, it reads the request body as a stream and concatenates the chunks into a string. Then, it uses the querystring
module to parse the form data into a JavaScript object. Finally, it logs the form data to the console and sends a response to the client.
Note that this example only works for form-urlencoded data. If you need to handle other types of request bodies (such as JSON), you'll need to use a different parser.
gistlibby LogSnag