weixin_37942128 2019-09-10 17:35 采纳率: 50%
浏览 491
已采纳

node express 中间件 无法往下执行

在使用中间件的时候,在完成一个中间件的时候,调用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 2019-09-11 09:16
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dabocaiqq 2019-09-10 22:10
    关注
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信
  • ¥15 使用QT实现can通信
  • ¥15 关于sp验证的一些东西,求告知如何解决,
  • ¥35 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题