Created
June 13, 2014 08:34
-
-
Save simenbrekken/4506526083efbafb3c5b to your computer and use it in GitHub Desktop.
RRouter Express Middleware
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
| var middleware = function(routes) { | |
| return function(req, res, next) { | |
| var path = req.path | |
| var query = req.query | |
| var match = Router.matchRoutes(routes, path, query) | |
| if (!match.route) return next() | |
| var props = lodash.reduceRight(match.activeTrace, function(props, trace) { | |
| return lodash.assign(props, trace.match) | |
| }, {path: path, query: query}) | |
| var view = match.route.view | |
| view.fetchData(props).then(function(data) { | |
| props.initialData = data | |
| var locals = { | |
| metadata: view.getMetadata ? view.getMetadata(data) : {}, | |
| markup: React.renderComponentToString(Application(Router.createView(match))), | |
| shared: JSON.stringify(props) | |
| } | |
| res.send(template(locals)) | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment