create a http server with a head route in typescript

Here's an example of how to create a basic HTTP server with a HEAD route in TypeScript:

index.ts
import http from 'http';

const port = 3000;

const server = http.createServer((req, res) => {
  if (req.method === 'HEAD') {
    res.writeHead(200, {
      'Content-Type': 'text/plain',
      'Content-Length': '0'
    });
    res.end();
  } else {
    res.statusCode = 404;
    res.end();
  }
});

server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});
392 chars
21 lines

In the code above, we're using the built-in http module to create an HTTP server that listens on port 3000. We're then checking the incoming request method - if it's a HEAD request, we're sending back a plain text response with a 200 status code and a Content-Length of 0. If it's not a HEAD request, we're sending back a 404 status code.

Note that this is just a basic example - you'd want to add more routes and functionality to the server depending on your specific use case. You could also use a web framework like Express or NestJS to handle routing and middleware.

gistlibby LogSnag