To create an HTTP server with a PUT route in Node.js, you can use the built-in http
module. Here's an example code snippet that creates an HTTP server with a PUT route:
index.tsx495 chars22 lines
In this code, we create a server using the createServer()
method of the http
module. Inside the server callback, we check if the request method is PUT
and the URL matches /put-data
. If this is true, we listen for a data event and concatenate the data to the body
variable, which we log to the console and send back as a response. If the method or URL don't match, we return a 404 error as the response. Finally, we listen for incoming requests on port 3000.
gistlibby LogSnag