-1

I am pretty new to the node.js and express.js landscape. However I have been trying to wrap my head around the code behind creating a site (20+ pages for example) in express.js without the code getting pretty large.

A route, when using a view engine, points the incoming request to index or whatever page they requested. After running the default express-generator with the view engine set to HBS you get something like

app.use('/', index);

This points to the index.js file under the routes folder that contains something like

router.get('/', function(req, res, next){
 res.render('index', {title: 'Express'});
});

And that then renders the index.hbs file to show what I want on the page.

My problem is this. Would you have to have a route for each page you wanted like a profile page, login page, about page, etc? If so wouldn't this create a lot of lines of code in routes if you had 20+ routes? Example would be in the app.js

app.use('a', a);
app.use('b', b);
...
app.use('z', z);

then each would require a corresponding route js file.

I assume there is a cleaner way of doing routing or perhaps I am over thinking this?

Any light on this concern/question of mine would be amazing.

Ben
  • 361
  • 2
  • 13
  • Can't you just put all the routes in one route.js file? – Frzn Flms Jan 04 '18 at 21:13
  • In theory you could group them together maybe that would cut down on the app.use statements required. I don't know what the standards are for routing in express.js but that would still require that you specifiy each route in the route js file. – Ben Jan 04 '18 at 21:17
  • The path supports parameters, a basic example is `app.get('/:page/', ...)`. You can reference that parameter using `req.params.page`. That way you won't have to state each route explicitly. –  Jan 04 '18 at 21:19

3 Answers3

1

It depends on how similar your routes are:

  • If they basically all have the same functionality, I'd put them in the same file.
  • If there are slight variations, I'd create a separate class containing the core functions, and then call to what ever is needed separately.
  • If they are completley different, put them all in separate files

This will give you a solid outline of how to do each:

1

In my express servers I break routes into different sections called "components" Each components can correspond to a give page if you're doing server-side rendering or it can correspond to a set of API routes.

Each component can have controllers to handle each route, and each controller can borrow from a handful of reusable actions.

Here's an example of an express server component I made(this example is for a set of API routes but the same architecture can be used for sets of hbs server-side rendering routes):

https://github.com/AkyunaAkish/react-redux-node-express-todo-list-boilerplate/tree/master/server/components/todos

Akyuna Akish
  • 147
  • 1
  • 7
-1

Generally, you make route files around areas of concern, though a given router can define whatever endpoints make sense. So for example, let's say your site has several pages that are fairly static (e.g. your "about" and "index" examples, and then several that are all based on blog entries (creating, listing, viewing, etc) and then several around users (user profiles and so on). You would probably create one router for each set of things, e.g.:

// ./routes/index.js
router.get('/', (req, res) => { res.render('index', {title: 'Express'}); });
router.get('/about', (req, res) => {res.render('about', {title: 'Express'});});
// etc

conceptually, you could use that more simply to pull the "index" or "about" values from the URI, but I'm assuming you'll do other things like assigning variables and such.

Then in another file

// ./routes/blog.js
router.get('/blog', (req, res) => { 
   // do whatever to fetch info fromt eh DB and render it... 
});

And so on.

Paul
  • 35,689
  • 11
  • 93
  • 122