Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom error page in loopback?

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.

like image 799
Robins Gupta Avatar asked Mar 20 '15 05:03

Robins Gupta


People also ask

What is loopback4?

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.

What is remote method in node JS?

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.


1 Answers

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();
            }
        });
    };
};
like image 160
dopeddude Avatar answered Oct 09 '22 02:10

dopeddude