title: Receive JSON with NodeJS
body: <p>With Node JS I'm trying to do simple user signup, sending <code>username</code>, <code>email</code> and <code>password</code> to the <code>'/newuser'</code> endpoint, which can be seen in <code>user.js</code>.
  The header gets sent correctly and I'm able to see it in the console, but I can not access the <code>req.body</code> part that I'm interested in. I'm using the <code>body-parser</code> express middleware as suggested in the documentation of express.</p>
  
  <p>If I print out <code>req.body</code>, only <code>[object Object]</code> is printed, if I print out the <strong>whole</strong> <code>req</code> object I can not see the parameters anywhere and if I try to <code>JSON.parse(req.body)</code>, then of course <code>SyntaxError: Unexpected token o in JSON at position 1</code> shows up. Can anyone point me in the right direction?</p>
  
  <h2>JavaScript on frontend</h2>
  
  <p>I'm doing it this way, and it should work like this (I've also tried it with ajax, but it also doesn't work, same error). But the data I'm sending is definitely correct, if I print it out before sending, it is a valid JSON.</p>
  
  <pre><code>var xhr = new XMLHttpRequest();
  xhr.open('POST', '/newuser');
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.onload = function() {
    console.log(xhr.responseText);
  };
  xhr.send(JSON.stringify({
    username: data.username,
    password: data.password,
    email: data.email
  }));
  </code></pre>
  
  <h2>server.js</h2>
  
  <pre><code>'use strict';
  
  var express = require('express');
  var hogan  = require('hogan-express');
  var http_module = require('http');
  var cors = require('cors');
  var bodyParser = require('body-parser');
  
  var app = express();
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json('application/json'));
  
  app.use(cors({credentials: true, origin: true}));
  app.engine('html', hogan);
  app.set('view engine', 'pug');
  app.set('views', __dirname + '/views');
  app.set('port', process.env.PORT || 4000);
  app.use(express.static(__dirname + '/public'));
  app.set('trust proxy', 1); // trust first proxy
  
  const partials = {
    header: 'partials/header',
    footer: 'partials/footer'
  };
  
  require('./routes')(app, partials);
  
  const http = http_module.Server(app);
  
  http.listen(app.get('port'), () =&gt; {
    console.info('Running on http://localhost:%s', app.get('port'));
  });
  
  module.exports = app;
  </code></pre>
  
  <h2>routes.js</h2>
  
  <pre><code>// Routes
  module.exports = (app, partials) =&gt; {
    require('./home')(app, partials);
    require('./signup')(app, partials);
    require('./user')(app, partials);
    require('./404')(app, partials);
  };
  </code></pre>
  
  <h2>user.js</h2>
  
  <pre><code>module.exports = (app, partials) =&gt; {
      app.post('/newuser', (req, res, next) =&gt; {
  
          console.log(req.header('Content-Type'))
          console.log(req.body) // this prints [object Object]
          console.log(JSON.parse(req.body)) // this throws SyntaxError: Unexpected token o in JSON at position 1
      });
  };
  </code></pre>