title: Handling bad/malicious API requests in NodeJS/Express
body: <p>I've created a basic api in node + express. I want to know how to handle bad JSON requests. I'm expecting JSON in my app. How do I check for incorrect requests, syntactically incorrect JSON or malicious things? I found if I sent syntactically incorrect JSON the node error would come out on the client side  of the api with the error details.</p>
  
  <p>API example:</p>
  
  <pre><code>app.post("/api", function(req, res) {
  
      if(typeof req.body === "undefined") {
          badReponse("No body");
      } else if ( (typeof req.body.something === "undefined") ){
          badReponse("Request attribute missing");
      }
  
      function badReponse (msg) {
          res.status(500).json({error: msg});
      }
  
  });
  </code></pre>