我很惊讶我找不到这个以前可能已经回答过了(我正在寻找错误的东西).
基本上,它是否可能,如何在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)