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.tsx297 chars14 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