title: Node.js express: why can't I read cookies ?
body: <p>I wrote a very simple node.js server: </p>
  
  <pre><code>var express = require('express');
  var cookieParser = require("cookie-parser");
  
  var app = express();
  app.use(cookieParser());
  
  app.get('/', function(req, res){
      res.sendFile(__dirname + "/hello.html");
  });
  
  app.get('/ajax', function(req, res) {
      console.log('ajax request received');
      console.log(req.cookies["username"])
  })
  app.listen(3000);
  </code></pre>
  
  <p>and a very simple client side html file: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt; Hello World &lt;/h1&gt;
      &lt;script
              src="https://code.jquery.com/jquery-3.2.1.min.js"
              integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
              crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script&gt;
          document.cookie = "username=John Doe";
          $.ajax({
              url: "http://127.0.0.1:3000/ajax",
          }).done(function() {
              alert('AJAX sent');
          });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>As you can understand, once the server runs and an end-user sends a get request to localhost:3000, the file 'hello.html' is served to client. The browser saves the cookie "username=John Doe" and sends AJAX request to endpoint localhost:3000/ajax. My intention is to read the content of the cookie in the server (I know that cookies are sent automatically from the client). However, req.cookies["username"] returns undefined. </p>
  
  <p>Do you know why? </p>