return a html response from a http server in javascript

Here's an example code snippet using Node.js' built-in http module to create a web server and send a response with HTML content:

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

const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.statusCode = 200;
  const htmlContent = '<html><body><h1>Hello World!</h1></body></html>';
  res.end(htmlContent);
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});
330 chars
13 lines

In this example, we create a server instance using http.createServer() and provide it with a request listener function that takes two arguments: the incoming HTTP request object and the response object that we'll use to send our HTML content.

We first set the content type header to 'text/html' and the response status code to 200 (OK). Then we create the actual HTML content as a string and send it to the client in the res.end() method.

If you run this code and navigate to http://localhost:3000 in your browser, you should see the "Hello World!" message displayed as a headline in your browser.

gistlibby LogSnag