To create an HTTP server and parse cookies and return a JSON response in TypeScript, you can use the built-in http and url modules of node.js along with the cookie-parser middleware.
Here is an example code that demonstrates how to do this:
index.ts782 chars26 linesIn this code, we created an HTTP server using the createServer method of the http module. We then used the cookie-parser middleware to parse cookies from the request headers.
We handled the request by checking the URL of the request. If it matches our desired endpoint, we returned a JSON response with a message saying "Hello World". If the URL does not match, we sent a 404 error response.
We set the Content-Type to application/json to indicate that we are returning JSON data. We used the writeHead method to set the response status code and headers, and the end method to write the response body.
Finally, we listened for incoming requests on port 3000 using the listen method of the http server.
Note: don't forget to run npm install cookie-parser to install the dependency.
gistlibby LogSnag