title: How to default to returning errors as JSON instead of HTML with express?
body: <p>Express by default returns errors as HTML pages. As I'm developing a REST api, I want all my errors to be in JSON format. How can i configure express for this?</p>
  
  <p>I expect the response to look something like this</p>
  
  <pre><code>{
    "statusCode": 404,
    "error": "Not Found"
  }
  </code></pre>
  
  <p>but instead I get</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Error&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;pre&gt;Cannot DELETE /object/&lt;/pre&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my basic app</p>
  
  <pre><code>export const app = express();
  app.use(express.json());
  
  app.get("/objects", listObjects);
  app.get("/object/:id", getObject);
  app.post("/object", createObject);
  app.put("/object/:id", updateObject);
  app.delete("/object/:id", deleteObject);
  </code></pre>
  
  <p>UPDATE:
  To clarify, it is not the errors from my handlers I want to handle as json. Doing that is pretty straight forward.</p>
  
  <p>What I'm after is for express to stop returning html errors for unregistered handlers, like doing DELETE on /assessments, or GET on /anUnknownRoute</p>