title: NodeJS - connect-keycloak middleware empty response error
body: <p>I'm having a problem using the connect-keycloak middleware with NodeJS, and there appears to be very little documentation online from people who have used it. This is based on 'A Full Example' from the official docs found here: <a href="http://keycloak.github.io/keycloak-nodejs/connect/" rel="nofollow noreferrer">http://keycloak.github.io/keycloak-nodejs/connect/</a></p>
  
  <p>I am getting an unexpected error when testing with curl, relating to an undefined 'keycloak-token'. I can't find any references to this in my code or the source, nor anyone else with the same problem online. Can anyone see what I'm doing wrong?</p>
  
  <p>The connect-keycloak object is included and instantiated as expected:</p>
  
  <pre><code>// app.js:
  // module dependencies
  var request = require('sync-request');
  var fs = require('fs');
  var restify = require('restify');
  var Keycloak = require('connect-keycloak');
  var session = require('express-session');
  var memoryStore = new session.MemoryStore();
  
  // Keycloak
  var keycloak = new Keycloak({ store: memoryStore });
  </code></pre>
  
  <p>And the middleware is used:</p>
  
  <pre><code>var server = restify.createServer({
      name: 'name',
      version: '1.0.0'
  });
  
  server.use(restify.acceptParser(server.acceptable));
  server.use(restify.queryParser());
  server.use(restify.bodyParser());
  server.use(keycloak.middleware({ logout: '/logout', admin: '/' }));
  
  server.use(session({
      secret: 'secret',
      resave: false,
      saveUninitialized: true,
      store: memoryStore
  }));
  </code></pre>
  
  <p>And the keycloak.protect method is in place:</p>
  
  <pre><code>server.get(/.*/, keycloak.protect(), restify.serveStatic({
      'directory': './html',
      'default': 'index.html'
  }));
  </code></pre>
  
  <p>Yet this curl test:</p>
  
  <pre><code>curl -H "Content-Type: application/json" -X POST -d '{"query":"test"}' http://localhost:3000/trust-me-on-the-url-being-correct/thanks -i
  </code></pre>
  
  <p>Produces this unusual error (not the error I was hoping for):</p>
  
  <pre><code> POST -d '{"query":"car"}' http://localhost:3000/rest/keywords -i
  HTTP/1.1 500 Internal Server Error
  Content-Type: application/json
  Content-Length: 87
  Date: Thu, 03 Dec 2015 02:05:40 GMT
  Connection: keep-alive
  
  {"code":"InternalError","message":"Cannot read property 'keycloak-token' of undefined"}[addamnilemartin@localhost keyword]$ 
  </code></pre>
  
  <p>Keycloak.json is included in the same directory as app.js and should definitely not be the cause of the problem.</p>
  
  <p>UPDATE:</p>
  
  <p>I realised this was missing and added it:</p>
  
  <pre><code>// set session for keycloak
  server.use(session({
      secret: 'fsd78d7gdfgds',
      resave: false,
      saveUninitialized: true,
      store: memoryStore
  }));
  </code></pre>
  
  <p>Now the response when my POST had keycloak.protect() the error is:</p>
  
  <pre><code>curl -H "Content-Type: application/json" -X POST -d '{"query":"car"}' http://localhost:3000/blah/blah -i
  curl: (52) Empty reply from server
  </code></pre>
  
  <p>Without keycloak.protect the response is the expected JSON, of course, as there is no attempt at authetication.</p>