Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was having a similar problem, but then remembered this is all "just javascript" and was able to muddle out an answer.</p> <p>If you want to have your routes defined in multiple files (instead of cramming them all into one routes/index.js file) you can just build the routes object in a hackish way (as follows):</p> <pre><code>var express = require('express') , routes = { index: require('./routes').index , events: require('./routes/events.js').events } , hbs = require('hbs'); </code></pre> <p>NOTE: You don't <em>need</em> the express and hbs definitions (first and last lines) in there, I just put it in there to give you a little context. This code snippet came directly from the top of my app.js file.</p> <p>Notice the <code>.index</code> and <code>.events</code> chained onto the <code>require()</code> function calls. That's the key. My events.js file only has one export (events):</p> <pre><code>exports.events = function(req, res){ console.log('in events'); res.render('events', { events: events, title: "EVENTS" }); console.log('events done'); }; </code></pre> <p>Since the <code>require()</code> function essentially grabs a file and requires (imports) any non-private vars (that is, those attached to the special <code>exports</code> object) and exposes them to the file containing the <code>require()</code> call, I'm able to just grab the specific function I'm requiring from the file I'm including with the <code>require()</code> call. If I had multiple exports defined in the required file, I imagine I could grab them like so (have not tested):</p> <pre><code>routes = { index: require('./routes').index , events: require('./routes/events.js').events , favorites: require('./routes/events.js').favorites , upcoming: require('./routes/events.js').upcoming } </code></pre> <p>I suspect that this would give someone with a bunch of nodeJS or MVC experience an aneurysm if they read your code (I'm betting that it would include the same file 3 times, but I'm not really sure). Maybe better off to do:</p> <pre><code>routes = { index: require('./routes').index , events: require('./routes/events.js').events , favorites: require('./routes/favorites.js').favorites , upcoming: require('./routes/upcoming.js').upcoming } </code></pre> <p>Otherwise, why not just shove them all in index? Not really sure though, this is only my second day working with Node and any of its related technologies...</p> <p>Also will probably help you if you throw a console.log statement right after your var declarations:</p> <pre><code>console.log(routes); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload