To create an HTTP server in JavaScript, you can use the built-in http
module in Node.js. Here's an example:
index.tsx270 chars11 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