Yan Li Yan Li - 6 months ago 16
Node.js Question

Node.js Express app.use('/route') run into other route?

In my application, I have 3 routes that are

http://localhost:8080/search
http://localhost:8080/user
http://localhost:8080/shop


I write routes in separated file, in server.js(the file run in the terminal):

app.use('/', require('./app/routes/index')(app,io,mongoose));


In the index.js:

app.use('/search', require('./searchRouter')(router));
app.use('/user', require('./user')(router,io,mongoose,app,express));
app.use('/shop', require('./shop')(router,io,mongoose,app,express));


They all have a route like:

router.route('/')
.get(function(req, res) {
res.send('routerRestuarant');
})

.post(function(req, res) {
res.send('routerRestuarant');
})

.put(function(req,res){
res.json({
msg:"new shop.js router"
})
});


problem:

When I send a request to route '/shop', I found the response come from '/search' and no response come from '/shop', how to fix it?

About pass the mongoose,app,express variables

Some part of my code is below:

Index.js:

var express = require('express');
//var router = express.Router({ mergeParams: true });
var router = express.Router();
var routeInit = function (app,io,mongoose) {
//app.use('sessions', require('./sessions')(router));

app.use('/test', require('./searchRouter')(router));
app.use('/user', require('./user')(router,io,mongoose,app,express));
app.use('/shop', require('./shop')(router,io,mongoose,app,express));
return router;
};

module.exports = routeInit;


Shop.js:

var routeShop = function (router,io,mongoose,app,express) {
var Shop = require('../models/Shop')(mongoose);

router.route('/createDish')
.post(function(req, res) {
var dish = req.param('dish', null);
var shopName = req.param('shopName', null);
for (var i = 0; i < dish.length; i++) {
Shop.addDish(shopName, dish[i], function(err) {
if (null == err)
res.json({
code: 200
});
})
}
})
return router;
};

module.exports = routeShop;


In Shop.js, I need the variable mongoose to init the mongoose schema, If i am not pass the variable mongoose here, the complier error is:

/Users/Documents/Dropbox/MScCS/fos/app/routes/shop.js:8
var Shop = require('../models/Shop')(mongoose);
^

ReferenceError: mongoose is not defined
at routeShop (/Users/liyan/Documents/Dropbox/MScCS/fos/food-ordering-by-zwy-liyan/food-ordering/app/routes/shop.js:8:38)

Answer

Try this way:

in server.js:

// defining components to be accessible from route handlers
app.use(function(req, res, next) {
  req.components = {
    io: io, 
    mongo: mongoose
  };
  next();
});

app.use(require('./app/routes/index'));

in index.js:

var 
  express = require('express'),
  router = express.Router();

router.use('/search', require('./searchRouter'));
router.use('/user', require('./user'));
router.use('/shop', require('./shop'));

module.exports = router; 

in router files:

var 
  express = require('express'),
  router = express.Router();

router.get('/', function(req, res) {
  req.components.io.emit('message', {data: "I'm in restaurant"});
  res.send('routerRestuarant');
});

router.post('/', function(req, res) {
  res.send('routerRestuarant');
})

router.put('/', function(req,res){
  res.json({
    msg:"new shop.js router"
  });
});

module.exports = router;