相关疑难解决方法(0)

快速默认路由参数

我很惊讶我找不到这个以前可能已经回答过了(我正在寻找错误的东西).

基本上,它是否可能,如何在nodejs express路由上设置默认值?

// Test route
router.route('/tests/:id')
    .get(testsController.tests.get);
Run Code Online (Sandbox Code Playgroud)

如果:id未设置if ,它将自动设置为任意值,例如1.

控制器代码:

var testsController = {
    tests: {
        get: function (req, res, next) {
            if (req.params.id) {
                res.render('tests.html', { title: 'The Site', id: req.params.id });
                next();
            } else {
                //res.redirect('/')
                console.log('here');
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我知道在PHP Symfony2中我可以这样做:

/**
 * @Route("/tests/{id}")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function testAction(Request $request, $id=1)
{
}
Run Code Online (Sandbox Code Playgroud)

routes node.js express

4
推荐指数
1
解决办法
3942
查看次数

标签 统计

express ×1

node.js ×1

routes ×1