在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ express中,get請(qǐng)求和post請(qǐng)求在use之后不能執(zhí)行。

express中,get請(qǐng)求和post請(qǐng)求在use之后不能執(zhí)行。

app.use('/', index);
app.use('/users', users);

//添加的post路由
app.post('/',function(req,res){
  res.send(req.body);
});
//添加的get路由
app.get('/about',function(req,res){
  res.send('about page');
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});
// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

這里的順序先use然后再get和post則成功添加路由,假如我把get和post移動(dòng)到最后才執(zhí)行則出錯(cuò)。搞不清楚這里的執(zhí)行機(jī)制,get和post路由是必須在use路由之后嗎?

clipboard.png

回答
編輯回答
悶油瓶

因?yàn)樵诤竺鎛ext來(lái)返回錯(cuò)誤,所以當(dāng)請(qǐng)求沒(méi)獲取到時(shí),直接就被攔截了而不會(huì)再去執(zhí)行g(shù)et的路由。。

2018年1月10日 03:19
編輯回答
短嘆

路由匹配是從上至下的,例如app.post('/')也是會(huì)先匹配到app.use('./')執(zhí)行,你說(shuō)執(zhí)行則出錯(cuò),到底是什么錯(cuò)誤也沒(méi)說(shuō)清楚。

2017年3月12日 04:54