create a http server in javascript

To create an HTTP server in JavaScript using node.js, you can use the built-in http module. Here's an example code snippet to create a simple HTTP server:

index.tsx
const http = require('http');

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, () => {
  console.log(`Server running at http://localhost:${port}/`);
});
297 chars
14 lines

In this example, we're creating a server that listens on port 3000. The createServer method takes a function as an argument that will be called every time a request is made to the server. Inside this function, we're setting the response status code, content type header and returning 'Hello World!' as the response body.

Finally, we call the listen method on our server object to start listening for incoming requests.

Note that you can replace 'Hello World!' with any other text or even HTML code that you want to serve as a response.

gistlibby LogSnag