return a html response from a http server in typescript

Here's an example of how you can return an HTML response from an HTTP server using TypeScript:

index.ts
import http from 'http';

const server = http.createServer((request, response) => {
  response.writeHead(200, { 'Content-Type': 'text/html' });
  response.write('<html><body><h1>Hello, World!</h1></body></html>');
  response.end();
});

server.listen(3000, () => {
  console.log('Server listening on port 3000');
});
317 chars
12 lines

In this example, we create an HTTP server using the http module that comes with Node.js. Inside the callback function that handles incoming requests, we set the response header to indicate that we are returning HTML content, and we write the HTML content to the response body.

Once we have finished writing the response, we call response.end() to signal to the server that we have finished sending the response back to the client.

We then call the listen() method on our server object to start listening for incoming requests on port 3000.

gistlibby LogSnag