Created
July 24, 2013 17:04
-
-
Save smithcommajoseph/6072438 to your computer and use it in GitHub Desktop.
What is the chain of middleware that fires off when Express responds to a Rendr route.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Building RendrRoutes | |
| // given | |
| [ '/', { controller: 'maps', action: 'index' }, [Function] | |
| //render.server.initApp.js (sole export) | |
| function (req, res, next) { | |
| var App, app; | |
| App = require(rendr.entryPath + '/app/app'); | |
| // Pass any config that needs to be accessible by the client | |
| // and server into the app. | |
| app = new App(appAttributes); | |
| // Hold on to a copy of the original request, so we can pull headers, etc. | |
| app.req = req; | |
| // Stash on the request so can be accessed elsewhere. | |
| req.rendrApp = app; | |
| next(); | |
| } | |
| //render.server.router.js (ServerRouter.prototype.getHandler ) | |
| function (req, res, next) { | |
| var app, context, params, redirect; | |
| params = router.getParams(req); | |
| redirect = router.getRedirect(route, params); | |
| /** | |
| * If `redirect` is present, then do a redirect and return. | |
| */ | |
| if (redirect != null) { | |
| res.redirect(route.status || 301, redirect); | |
| return; | |
| } | |
| app = req.rendrApp; | |
| context = { | |
| currentRoute: route, | |
| app: app, | |
| redirectTo: function(url) { | |
| res.redirect(url); | |
| } | |
| }; | |
| action.call(context, params, function(err, viewPath, locals) { | |
| if (err) return router.handleErr(err, req, res); | |
| var defaults = router.defaultHandlerParams(viewPath, locals, route); | |
| viewPath = defaults[0], locals = defaults[1]; | |
| var viewData = { | |
| locals: locals || {}, | |
| app: app, | |
| req: req | |
| }; | |
| res.render(viewPath, viewData, function(err, html) { | |
| if (err) return router.handleErr(err, req, res); | |
| res.set(router.getHeadersForRoute(route)); | |
| res.type('html').end(html); | |
| }); | |
| }); | |
| } | |
| // server.middleware.errHandler.js (sole export) | |
| function errorHandler(err, req, res, next) { | |
| if (err.status === 401) { | |
| res.redirect('/login'); | |
| } else if (err.status === 404) { | |
| handle404()(req, res, next); | |
| } else { | |
| express.errorHandler()(err, req, res, next); | |
| } | |
| } | |
| // ends w/ ... | |
| app.get(path, fnChain); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment