首页 > 路由参数为空时访问默认数据

路由参数为空时访问默认数据

路由怎么写才能让type为空时访问到默认的数据
type = ' '时:http://abc.com/user/1002
(现在的情况是,type为空时报错)
type = friend 时:http://abc.com/user/1002/friend

app.get('/user/:id/:type', function (req, res, next) {
  console.log('although this matches');
  next();
});

type='/'


在你的这个路由前面补一个跳转路由吧:

app.get('/user/:id', function (req, res) {
    res.redirect('/user/' + req.params.id + '/默认值');
});

注意,一定要加在你的路由前面

【热门文章】
【热门文章】