Friday, 17 March 2017

Express routing .get and .use

i've seen similar question but didnt quite get the answer.
In my Express application i have : "app.js", "users.js" and "index.js"

"app.js" looks something like this :

var express = require ('express') 
var index = require('./routes/index.js') 
var users = require('./routes/users.js')
app.get('/users',users)
app.get('/index',index)
app.get('/',index)
app.listen etc...

users.js and index.js are simple modules exporting a express.Router() instace. For example "users.js" is something like this :

router =express.Router()  
router.use('/admin',admin) //some more modules i wont add here for concision  
router.get('/',function(req,res,next){log some stuff  
next()})  

Now if i launch the application and send something at localhost:3000/users will not call the function inside users.js. BUT if i switch to

app.use('/users',users)  
app.use('/index',index)  
app.use('/',index)  

inside app.js and call localhost:3000/users the function i mentioned above will be called. How so?



via Filippo Bosco

No comments:

Post a Comment