第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Express js 中的 next() 去了哪里?

Express js 中的 next() 去了哪里?

慕碼人2483693 2023-09-21 10:41:30
我對 javascript、nodejs 和express 很陌生,并且對使用next().我希望我的代碼轉(zhuǎn)移到下一個路由器next(),但它似乎轉(zhuǎn)移到下一個then。我的代碼://validationrouter.post('/requests', (req, res, next) => {let {myData} = req.bodybasicCheck(res, cluster, myData)        .then(() => {            if (myCheck()) {                next()                return  // Doesn't need to do rest of the code. Just move on to the next router.post            }            ...            return Promise.all(somePromises)        })        .then(() => {            ...            return Promise.all(somePromises)        })        .then(() => {            if (someCheck() {                next()            } else {                res.status(400).send('message') // My code reaches here! even when myCheck() is true            }        })        .catch((err) => {            ...        })})// where next() needs to berouter.post('/requests', (req, res) => {    ...})當(dāng)next()在 之外時basicCheck,next()轉(zhuǎn)到下一個 router.post。我不明白哪里指示的概念next()。myCheck()在內(nèi)部執(zhí)行操作時如何更正此代碼basicCheck()?
查看完整描述

1 回答

?
慕的地8271018

TA貢獻1796條經(jīng)驗 獲得超4個贊

隨著next()您移動到下一個中間件。


示例:


你有這樣的路線:


app.get("/", (req, res, next) => {

   res.send("hello")

})

您可以聲明一個函數(shù)并使用它,而不是使用匿名函數(shù),如下所示:


function firstMiddleware(req, res, next){

   res.send("hello")

}


app.get("/", firstMiddleware);

您可以做的是您的路線中可以有多個中間件,例如:


function firstMiddleware(req, res, next){

   console.log("hy");

   next()

}


function secondMiddleware(req,res,next) {

   console.log("hello")

   res.send("hello");

}


app.get("/", firstMiddleware, secondMiddleware);

如你看到的。在我的第一個中間件中,我使用next(). 在這種情況下,這告訴express.js移動到下一個中間件secondMiddleware


中間件從左到右執(zhí)行,next()您告訴它們移動到下一個,直到最后。


通常最后一個中間件是您的 API 端點,您不應(yīng)該使用,next()否則您會“跳出”您的路線,并且如果您定義了全局錯誤處理程序,您將收到錯誤


另請注意:一個好處是通過創(chuàng)建一個名為controller.jsexample 的文件來分離您的路線和邏輯。


控制器.js


function firstMiddleware(req, res, next){

   console.log("hy");

   next()

}


function secondMiddleware(req,res,next) {

   console.log("hello")

   res.send("hello");

}


module.exports = {

   firstMiddleware,

   secondMiddleware

}

現(xiàn)在您可以導(dǎo)入它:


const { firstMiddleware, secondMiddleware } = require("./controller.js");


app.get("/", firstMiddleware, secondMiddleware);

這使得您的代碼隨著代碼的增長而更容易維護


編輯:


router.post("/requests", async (req, res, next) => {

  let { myData } = req.body;

  let checkresult = await awbasicCheck(res, cluster, myData);


  if (myCheck()) {

    return next();

  }


  let someResults = await Promise.all(somePromises);

  let someMoreResults = await Promise.all(somePromises);


  if (someCheck()) {

    return next();

  } else {

    res.status(400).send("message"); // My code reaches here! even when myCheck() is true

  }

});

您使用return“是”停止函數(shù)的執(zhí)行,但您還做的是承諾鏈接。


我在這里寫了一個 async/await 方法


查看完整回答
反對 回復(fù) 2023-09-21
  • 1 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號