weixin_37942128
weixin_37942128
采纳率50%
2019-09-10 17:35

node express 中间件 无法往下执行

5
已采纳

在使用中间件的时候,在完成一个中间件的时候,调用next(),无法往下执行,直接跳出所有的路由,执行了无法找到路由(404)。

哪位大佬帮我这个菜鸟看看,感激不尽,谢谢!

middleware.js

middleware.test = async (req, res, next) => {
  console.log('test');
  await next();
  console.log(123123);
};

middleware.test1 = async (req, res, next) => {
  console.log('test1');
  await next();
  console.log(1231234444);
};
routes.js

const userList = (req, res) => {
  console.log(123123)
};

Router.post('/userList', [MiddleWare.test, MiddleWare.test1], userList);

执行后报错:

test
123123
POST /api/admin/user/userList 404 12.268 ms - 1226
[2019-09-10T17:21:44.093] [ERROR] admin_server.js - ::ffff:127.0.0.1 - - "POST /api/admin/user/userList HTTP/1.1" 404 1226 "" "PostmanRuntime/7.16.3"
NotFoundError: Not Found
    at F:\wpc\project\smart\smart\admin_server.js:63:8
    at Layer.handle [as handle_request] (F:\wpc\project\smart\smart\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:317:13)
    at F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:335:12)
    at next (F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle [as handle_request] (F:\wpc\project\smart\smart\node_modules\express\lib\router\layer.js:91:12)
    at trim_prefix (F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:317:13)
    at F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (F:\wpc\project\smart\smart\node_modules\express\lib\router\index.js:335:12)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • weixin_37942128 weixin_37942128 2年前

    已解决,我自己封装的路由产生的影响。

    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 2年前