create a http server and add a custom header to the response in javascript

To create an HTTP server in JavaScript, you can use the built-in http module in Node.js. Here's an example:

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

const server = http.createServer((request, response) => {
  response.setHeader('Custom-Header', 'Hello World');
  response.end('Hello World');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});
270 chars
11 lines

In the code above, we create a new HTTP server by calling the createServer method of the http module. We pass in a callback function that will be called for every incoming request.

Within the callback function, we add a custom header to the response using the setHeader method. We set the Custom-Header to Hello World.

Finally, we end the response with the text Hello World.

We start the server by calling the listen method of the server object and specifying the port number we want to listen on (in this case, 3000).

When you run this code, you can access the server by going to http://localhost:3000/ in your web browser. If you check the network tab in your browser's developer tools, you'll see that the Custom-Header is present in the response headers.

gistlibby LogSnag