Parsing JSON in Express without BodyParser
<p>I'm trying to write a simple express server that takes incoming JSON (POST), parses the JSON and assigns to the request body. The catch is I cannot use bodyparser. Below is my server with a simple middleware function being passed to app.use</p>

<p>Problem: whenever I send dummy POST requests to my server with superagent (npm package that lets you send JSON via terminal) my server times out. I wrote an HTTP server in a similar fashion using req.on('data')...so I'm stumped. Any advice?</p>

<pre><code>const express = require('express');
const app = express();

function jsonParser(req, res, next) {
  res.writeHead(200, {'Content-Type:':'application/json'});
  req.on('data', (data, err) =&gt; {
    if (err) res.status(404).send({error: "invalid json"});
    req.body = JSON.parse(data);
  });
  next();
};

app.use(jsonParser);
app.post('/', (req, res) =&gt; {
  console.log('post request logging message...');
});

app.listen(3000, () =&gt; console.log('Server running on port 3000'));
</code></pre>