desoares
desoares

Reputation: 13

Express js sending app variable to routes

I'm trying to send some data from the app (var app = express();) variable to some code Socket.IO related and sending a value to some middleware kind of the same way:

function routes(app) {
    app.post('/evento', function (req, res) {
        evento.novoEvento(req, res, app);//passing app to the method evento.novoEvento
    });
    app.put('/evento', function (req, res) {
            restringeAcao(req, res, 'Evento');//passing the string 'Evento' to the method evento.alteraEvento
        }, evento.alteraEvento);
    });

}

It does work but I never seen any example like this. My question is: is there a reason no one uses it like this? If so what is the right way to do it?

Upvotes: 0

Views: 69

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 145994

Express already has a property on both req.app and res.app you can use to access the application object the contains the request and response. Thus any route handler function or middleware has easy access to the app instance.

Upvotes: 1

Related Questions