To create a HTTP server with a specific route and a specific status code, you can use Node.js built-in http
module. Here is an example:
index.tsx472 chars18 lines
In the above code, we create a HTTP server that listens on port 3000. The server has a single route /foo
. If the requested URL is /foo
, the server responds with a 200 OK
status code and Hello, foo!
message. Otherwise, the server responds with a 404 Not Found
status code and an empty response body.
You can test the server by opening a web browser and navigating to http://localhost:3000/foo
. You should see the message Hello, foo!
displayed on the page. If you navigate to another URL, you should see a 404 Not Found
error.
gistlibby LogSnag