apinf / platform

Apinf - Open source API management platform with multi proxy and protocol support
https://apinf.com/
European Union Public License 1.1
74 stars 33 forks source link

Return http error code 404 when resource not found #3357

Closed anarva closed 6 years ago

anarva commented 6 years ago

Title:

"Return 404 error code if the requested resource or route does not exist on the server."

Description:

Currently the Meteor app returns a configuration document and http code 200 if a requested document does not exist. There is a discussion about this on flow-router (which is used for apinf) GIthub project page: https://github.com/kadirahq/flow-router/issues/84

Sample code:

if(Meteor.isServer) {
    WebApp.connectHandlers.use('/', function(req, res, next) {
        if(isValidRoute(req.url)) 
            return next();

        res.writeHead(404);
        res.end(SSR.render('404'));
    });

    var pathToRegexp = Meteor.npmRequire('path-to-regexp')

    function isValidRoute(requestUrl) {
        let splitUrl = requestUrl.split('?')[0];
        for(var i = 0; i < FlowRouter._routes.length; i++)
            if(pathToRegexp(FlowRouter._routes[i].path).test(splitUrl))
                return true;

        return false;
    }
}

User Story:

"As a user of the Meteor app or as a REST API consumer, I want to receive a proper error code if the requested resource is not found."

ilarimikkonen commented 6 years ago

implemented, closing