Skip to content

Instantly share code, notes, and snippets.

@harmenjanssen
Last active June 30, 2016 10:12
Show Gist options
  • Select an option

  • Save harmenjanssen/0b35bb6e20f254dba43fdf2878924131 to your computer and use it in GitHub Desktop.

Select an option

Save harmenjanssen/0b35bb6e20f254dba43fdf2878924131 to your computer and use it in GitHub Desktop.
Reduced test-case showing the problem with sharing routes between Router instances
<p>This is the homepage.</p>
<a href="<% url('home') %>">A link to myself</a>
<!--
This will throw an error: "No route found with the name:home"
-->
// the app's main point of entry
const express = require('express');
const app = express();
const namedRouter = new (require('named-routes'));
namedRouter.extendExpress(app);
namedRouter.registerAppHelpers(app);
app.use('/', require('./routes'));
app.set('views', './views');
app.set('view engine', 'ejs');
app.listen(8000, _ => {
console.log('Example app listening on port 8000!');
});
// routes.js contains route configuration
// other routes files can be included from here to keep files a bit cleaner
//
// note that I cannot access my app instance from here
const router = require('express').Router();
const namedRouter = new (require('named-routes'));
namedRouter.extendExpress(router);
router.get('/', 'home', (req, res) => res.render('home'));
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment