I want to define custom 404 not found response pages with loopback. In documentation it's been given that loopback's middleware has been defined on express but i am not getting how to define custom error page in loopback.
LoopBack is an award-winning, highly extensible, open-source Node. js and TypeScript framework based on Express. It enables you to quickly create APIs and microservices composed from backend systems such as databases and SOAP or REST services.
A remote method is a method of a model, exposed over a custom REST endpoint. Use a remote method to perform operations not provided by LoopBack's standard model REST API. Note: The easiest way to define a remote method is by using the command-line remote method generator.
Inside the middleware.json, remove the loopback#urlNotFound
middleware from the final phase and update it with this:
"final": {
"./middleware/url-not-found-handler": {}
},
Now, place the following content on the file server/middleware/url-not-found-handler.js
'use strict';
module.exports = function () {
//4XX - URLs not found
return function customRaiseUrlNotFoundError(req, res, next) {
res.sendFile('path to 404.html', function (err) {
if (err) {
console.error(err);
res.status(err.status).end();
}
});
};
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With