title: How to handle authentication header using node.js?
body: <p>I was creating a token authentication server using jsonwebtoken.</p>
  <p>my index_client.js code</p>
  <pre><code>$.ajax({
      type: 'GET',
      url: '/:id',
      beforeSend: function (xhr) {
          xhr.setRequestHeader(&quot;Content-type&quot;,&quot;application/json&quot;);
          xhr.setRequestHeader(&quot;Authorization&quot;, &quot;Bearer &quot; + localStorage.getItem('accessToken'));
      },
      datatype: 'json',
      success: function(result) {
      },
  });
  </code></pre>
  <p>Get an access token from local storage and set the authentication header.</p>
  <p>my mainRouter.js</p>
  <pre><code>router.get('/:id', checkToken, function(req, res, next) {
      res.sendFile(path.join(__dirname, '..', '..', '/public/views/test.html'));
  });
  </code></pre>
  <p>my checkToken.js</p>
  <pre><code>module.exports = function(req, res, next) {
      if (req.headers.authorization) {
           console.log(req.headers.authorization);
           next();
      }
  }
  </code></pre>
  <p>A problem occurred with the next() method.</p>
  <p>When running the server and connecting with /:id url
  The page is blank and the page loads indefinitely.</p>
  <p>my console message</p>
  <pre><code>undefined
  GET /123 304 10.042 ms - -
  GET /index_client.js 304 2.249 ms - -
  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Imp1bjA5MTEiLCJpYXQiOjE2MzAyMTA0NTksImV4cCI6MTYzMDIxNDA1OSwiaXNzIjoiY290YWsifQ.nYnr862J2v8NjpIv287BUAXPdo-aYLSylmV4R0FQXDI
  undefined
  GET /favicon.ico 304 11.232 ms - -
  </code></pre>
  <p>Why am I getting the console message 3 times and the access token only once?</p>
  <p>I searched for an answer to this, but couldn't find it.</p>
  <p>Thanks in advance!</p>