To create an HTTP server in Typescript, we need to make use of the built-in http
module. Here's an example code block to create an HTTP server and read the request body as HTML using body-parser
in Typescript:
index.ts599 chars25 lines
In this example, we first import the necessary modules - http
, express
, and body-parser
. Then, we create an instance of express
, which we'll use to handle our routes. We then set up body-parser
to parse the request body.
Next, we create an HTTP server using http.createServer
, passing in our express
app instance. We then define a route to handle POST requests sent to '/submit'. When a POST request is received, we read the HTML from the request body using req.body.html
, log it to the console, and send a response back to the client.
Finally, we start the server listening on port 3000 using server.listen
.
Note that this example assumes that we're receiving a POST request with a JSON payload that includes an html
property. If the request body is different, the code will need to be adjusted accordingly.
gistlibby LogSnag