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?
I expect the response to look something like this
{
"statusCode": 404,
"error": "Not Found"
}
but instead I get
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot DELETE /object/</pre>
</body>
</html>
This is my basic app
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);
UPDATE: To clarify, it is not the errors from my handlers I want to handle as json. Doing that is pretty straight forward.
What I'm after is for express to stop returning html errors for unregistered handlers, like doing DELETE on /assessments, or GET on /anUnknownRoute