create a http server and parse cookies in javascript

To create an http server and parse cookies in Node.js, you can use the built-in http module and the cookie module. Here's an example:

index.tsx
const http = require('http');
const cookie = require('cookie');

http.createServer((req, res) => {
  // Parse cookies from the request headers
  const cookies = cookie.parse(req.headers.cookie || '');

  // Set a cookie
  res.setHeader('Set-Cookie', cookie.serialize('mycookie', 'value'));

  // Write the response
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello, World!');
  res.end();
}).listen(3000);

console.log('Server running at http://localhost:3000/');
487 chars
18 lines

In this example, we use the http module's createServer method to create an HTTP server. When a request is received, we parse any cookies from the request headers and serialize a new cookie to set in the response headers using the cookie module's parse and serialize methods. Finally, we write a simple response to the client.

Note that this is a basic example and you may want to add error handling and/or middleware to handle more complex scenarios.

gistlibby LogSnag