node.js - Express route handler based with custom parameters -
i'd output either html or json based on req.headers data. example: if req.headers.contenttype == "application/json" return json, otherwise html.
var route = { html: function(req, res, next) {}, json: function(req, res, next) {} } app.get('/test', route);
this doesn't work. figured i'd need have intermediary function:
app.get('/test', _findroute); function _findroute(req, res, next) { if(req.headers["content-type"] === "application/json") { return route.json; } else { return route.html; } }
which doesn't work because don't have access route object @ point.
i can do:
app.get('/text', _findroute(route));
but don't have access req object.
i have no idea how proceed, ideas welcome :)
the last version (app.get('/text', _findroute(route));
) work if rewrite _findroute little.
function _findroute (route) { return function (req, res, next) { if(req.headers["content-type"] === "application/json") { route.json(req, res, next); } else { route.html(req, res, next); } } }
Comments
Post a Comment