To create an HTTP server with a specific route in JavaScript, you can use the built-in Node.js http
module and create a Server
object. Here's an example:
index.tsx411 chars16 lines
In this example, we create a server and specify the route /mypage
. When a client requests this route, the server sends a response with a 200
status code and the string <h1>Hello World!</h1>
as the HTTP response body. For any other route, the server sends a 404
Not Found response.
You can modify this code to create as many routes as you need by adding more if statements inside the callback function of createServer
.
gistlibby LogSnag