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.
How to define custom error page in loopback?
Asked Answered
Since loopbackjs uses express, you would do it the same way you do with express. expressjs.com/guide/error-handling.html –
Hepza
It looks like the error handling example is missing from the loopback doc at docs.strongloop.com/display/public/LB/… . So it's not clear how the loopback specific config should be done relative to what phase it should be in and how it relates to the loopback urlNotFound and express errorhandler middleware. –
Oligarchy
Hey even i am not able to get a proper solution for the same. –
Abomination
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();
}
});
};
};
Not sure if this is something that worked in LoopBack v2, but in LoopBack v3 the function that is being returned needs to start with an
err
field. So customRaiseUrlNotFoundError(err, req, res, next)
–
Corvette © 2022 - 2024 — McMap. All rights reserved.