I have routes in separate folder for expressjs. The setup is working fine for the 'index' page, but not for any additional routes.
This is my index.js, inside my routes folder.
module.exports = function(db) {
return {
index: function(req, res, next) {
res.send('index');
}
}
}
This is my join.js, inside my routes folder.
module.exports = function(db) {
return {
join: function(req, res, next) {
res.send('join');
}
}
}
In my app.js, I define my routes like this:
var routes = require('./routes')(db);
app.get('/', routes.index);
app.get('/join', routes.join);
When I go to http://localhost:3000
but when I go to http://localhost:3000/join
i get Cannot GET /join
If I define my route for join like this:
app.get('/join', function(req, res){
res.send('join 2');
});
This works.
Any idea what I'm doing wrong here?
Thank you!
Once the routes and controllers files are created we need to tell Express to use those routes Noted, the app.use () have routes prefix in '/api/products' that mean all url path in the routes file will include that prefix automatically.
First we create routes for a wiki in a module named wiki.js. The code first imports the Express application object, uses it to get a Router object and then adds a couple of routes to it using the get () method. Last of all the module exports the Router object. Note: Above we are defining our route handler callbacks directly in the router functions.
For anyone familiar with working in NodeJS, usually your first starter point when pulling any new code is to look at the package.json and/or index.js. The first thing you'll see is that the top level index.js only has one job - to start the server. "Controllers" are used to define how the user interacts with your routes.
I have been working on the backend with NodeJS for a while now as both a hobbyist and as a professional. Thus, I've been exposed to both open-source code as well as enterprise level Javascript code for NodeJS. Last year I led the rearchitecture for one of Intuit's products from .NET to NodeJS, which you can read about here.
I was having a similar problem, but then remembered this is all "just javascript" and was able to muddle out an answer.
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):
var express = require('express')
, routes = {
index: require('./routes').index
, events: require('./routes/events.js').events
}
, hbs = require('hbs');
NOTE: You don't need 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.
Notice the .index
and .events
chained onto the require()
function calls. That's the key. My events.js file only has one export (events):
exports.events = function(req, res){
console.log('in events');
res.render('events', { events: events, title: "EVENTS" });
console.log('events done');
};
Since the require()
function essentially grabs a file and requires (imports) any non-private vars (that is, those attached to the special exports
object) and exposes them to the file containing the require()
call, I'm able to just grab the specific function I'm requiring from the file I'm including with the require()
call. If I had multiple exports defined in the required file, I imagine I could grab them like so (have not tested):
routes = {
index: require('./routes').index
, events: require('./routes/events.js').events
, favorites: require('./routes/events.js').favorites
, upcoming: require('./routes/events.js').upcoming
}
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:
routes = {
index: require('./routes').index
, events: require('./routes/events.js').events
, favorites: require('./routes/favorites.js').favorites
, upcoming: require('./routes/upcoming.js').upcoming
}
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...
Also will probably help you if you throw a console.log statement right after your var declarations:
console.log(routes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With