create a http server on a specific port and host in typescript

To create a HTTP server on a specific port and host in TypeScript, you will need to use the built-in Node.js http module. Here's an example code block that demonstrates how to create a basic HTTP server:

index.ts
import * as http from 'http';

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!');
});

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

In this example, we import the http module and define a hostname and port for our server. We then create a server using the http.createServer() method, which takes a callback function as an argument that handles incoming HTTP requests. In this example, our callback function simply sets a status code, content type, and message in the response.

Finally, we use the server.listen() method to start the server and specify the port and hostname on which it should listen for incoming requests. Once the server is running, it will log a message to the console indicating the URL at which it can be accessed.

Note that this is just a basic example, and there are many ways to customize and extend your HTTP server using TypeScript and Node.js.

gistlibby LogSnag