如何将参数传递给express.js路由器?

时间:2015-05-14 08:01:15

标签: javascript node.js express

以下是Express.js routing guide的修改示例:

private void FindVisibleItems(ListBox listbox) 
    { 
        var listboxRectangle = new Rect(new Point(0, 0), listbox.RenderSize); 
        for (int index = 0; index < listbox.Items.Count; index++) 
        { 
            double visiblePercent = 0; 

            ListBoxItem item = listbox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem; 
            if (item != null) 
            { 
                var itemTransform = item.TransformToVisual(listbox); 
                var itemRectangle = itemTransform.TransformBounds(new Rect(new Point(0, 0), item.RenderSize)); 
                itemRectangle.Intersect(listboxRectangle); 

                if (!itemRectangle.IsEmpty) 
                { 
                    visiblePercent = itemRectangle.Height / item.RenderSize.Height * 100; 
                } 
            } 

            System.Diagnostics.Debug.WriteLine(string.Format("Item {0}: {1}% visible", index, Math.Round(visiblePercent))); 
        } 
    }

这打印&#34;关于鸟类&#34;当我同时访问var express = require('express'); var router = express.Router(); router.get('/', function(req, res) { res.send('Birds home page'); }); router.get('/about', function(req, res) { res.send('About birds'); }); ... app.use('/birds', router); app.use('/fish', router); /birds/about时。

如何将参数或其他东西传递给路由器,这样,在控制器功能中,它可以区分这两条不同的路径?

例如,我希望看到#34;鸟类可以飞行&#34;访问/fish/about和&#34;鱼可以游泳&#34;访问/birds/about时。

理想情况下,我希望能够传递一些&#34;配置对象&#34;因此迷你应用程序不需要知道它可能安装在所有可能的路径上(伪代码):

/fish/about

5 个答案:

答案 0 :(得分:11)

以下是我的想法:我将“mini-app配置”分配给req

app.use('/birds', function (req, res, next) {
    req.animal_config = {
        name: 'Bird',
        says: 'chirp'
    };
    next();
}, animal_router);

app.use('/cats', function (req, res, next) {
    req.animal_config = {
        name: 'Cat',
        says: 'meow'
    }
    next();        
}, animal_router);

然后在我的路线中我可以访问它们:

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

...

router.get('/about', function(req, res) {
  var animal = req.animal_config;
  res.send(animal.name + ' says ' + animal.says);
});

这种方法可以轻松地将“迷你应用”安装在另一个提供不同配置的位置,而无需修改应用代码:

app.use('/bears', function (req, res, next) {
    req.animal_config = {
        name: 'Bear',
        says: 'rawr'
    };
    next();
}, animal_router);

答案 1 :(得分:5)

所以,如果你想通过url提供更改,那么你可以注入这样的参数:

router.get('/:animal/about', function(req, res) {
    // here we have bird or fish in req.params.animal
    if(req.params.animal == 'bird') {
        res.send('Birds can fly');
    } else if(req.params.animal == 'fish') {
        res.send('Fish can swim');
    } else {
        res.send('Unknown animal');
    }
});
app.use('/', router);

答案 2 :(得分:4)

您基本上是在谈论将配置注入路由器。

我遇到了类似的问题,并发现从理论上讲,您不能导出路由器本身,而可以导出接受配置并返回创建和配置的路由器的功能。

因此,在您的情况下,调用代码将如下所示:

var animal_router = require('./animal_router')

app.use('/birds', animal_router({
    name: 'Bird',
    says: 'chirp'
}));

app.use('/cats', animal_router({
    name: 'Cat',
    says: 'meow'
}));

./animal_router.js可能如下所示:

var express = require('express');

// Create wrapper function that will adjust router based on provided configuration
var wrapper = function (animal_config) {
    var router = express.Router();
    
    router.get('/about', function(req, res) {
        var animal = animal_config;
        res.send(animal.name + ' says ' + animal.says);
    });

    return router;
}

module.exports = wrapper;

答案 3 :(得分:1)

您可以使用req.baseUrl来解决这个问题。

答案 4 :(得分:0)

您可以像这样添加路线参数:

router.get('/about/:param1/:param2', function(req, res) {
 //then you can call this handler  through /about/1/sometext get these params from request object:
  console.log(req.params.param1, req.params.param2); // 1, 'sometext'
  res.send('About birds');
});

或者您可以通过查询参数发送参数:

router.get('/about', function(req, res) {
 //then you can call this handler  through /about?param1=1&param2=sometext get these params from request object as well:
  console.log(req.query.param1, req.query.param2); // 1, 'sometext'
  res.send('About birds');
});