Node.js 如何在中间件中等待next()

Node.js 如何在中间件中等待next(),node.js,express,Node.js,Express,使用express,我有这样一个中间件: app.use((req, res, next) => { console.log(req); next(); }); 如何等待下一个()完成? 我询问的原因是我在next()之后添加了console.log,此消息发生在next route函数中的消息之前 app.use(async(req, res, next) => { console.log(req); await next(); console.log('sh

使用express,我有这样一个中间件:

app.use((req, res, next) => {
  console.log(req);
  next();
});
如何等待下一个()完成? 我询问的原因是我在next()之后添加了console.log,此消息发生在next route函数中的消息之前

app.use(async(req, res, next) => {
  console.log(req);
  await next();
  console.log('should be the last print but ...');
});

我在我的项目中遇到了这个问题,我们快速地将中间件一分为二。在路由之前添加第一个部分,并在之后添加的另一个部分中的“next()”之后添加要执行的内容。如果需要访问相同的对象实例或其他对象,您可以始终将其保存在请求中

我的例子是:


const express = require('express');
const app = express();
const port = 5050;
const wait = (milliseconds) =>
    new Promise((res, rej) => {
        setTimeout(() => {
            res();
        }, milliseconds);
    });

const middleware = async (req, res, next) => {
    console.log('- 1');
    await wait(10);
    console.log('- 2');
    next();
    console.log('- 3');
    await wait(10);
    console.log('- 4');
    console.log('');
};
app.use(middleware);
app.get('/', async (req, res, next) => {
    console.log('-- 1');
    await wait(10);
    console.log('-- 2');
    console.log('hello');
    res.send('Hello World!');
    console.log('-- 3');
    await wait(10);
    console.log('-- 4');
    next();
    return;
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});
这将导致您遇到的问题。它会在这些行中打印一些东西

- 1
- 2
-- 1
- 3
-- 2
hello
-- 3
- 4

-- 4
解决方案:

const express = require('express');
const app = express();
const port = 5050;
const wait = (milliseconds) =>
    new Promise((res, rej) => {
        setTimeout(() => {
            res();
        }, milliseconds);
    });

const middleware1 = async (req, res, next) => {
    console.log('- 1');
    await wait(10);
    console.log('- 2');
    next();
};

const middleware2 = async (req, res, next) => {
    console.log('- 3');
    await wait(10);
    console.log('- 4');
    console.log('');
    next();
};

app.use(middleware1);
app.get('/', async (req, res, next) => {
    console.log('-- 1');
    await wait(10);
    console.log('-- 2');
    console.log('hello');
    res.send('Hello World!');
    console.log('-- 3');
    await wait(10);
    console.log('-- 4');
    next();
    return;
});
app.use(middleware2);

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});
您将中间件一分为二,以确保只有在执行路由中的所有内容之后才执行中间件2。您将得到如下输出:

- 1
- 2
-- 1
-- 2
hello
-- 3
-- 4
- 3
- 4

我在我的项目中遇到了这个问题,我们快速地将中间件一分为二。在路由之前添加第一个部分,并在之后添加的另一个部分中的“next()”之后添加要执行的内容。如果需要访问相同的对象实例或其他对象,您可以始终将其保存在请求中

我的例子是:


const express = require('express');
const app = express();
const port = 5050;
const wait = (milliseconds) =>
    new Promise((res, rej) => {
        setTimeout(() => {
            res();
        }, milliseconds);
    });

const middleware = async (req, res, next) => {
    console.log('- 1');
    await wait(10);
    console.log('- 2');
    next();
    console.log('- 3');
    await wait(10);
    console.log('- 4');
    console.log('');
};
app.use(middleware);
app.get('/', async (req, res, next) => {
    console.log('-- 1');
    await wait(10);
    console.log('-- 2');
    console.log('hello');
    res.send('Hello World!');
    console.log('-- 3');
    await wait(10);
    console.log('-- 4');
    next();
    return;
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});
这将导致您遇到的问题。它会在这些行中打印一些东西

- 1
- 2
-- 1
- 3
-- 2
hello
-- 3
- 4

-- 4
解决方案:

const express = require('express');
const app = express();
const port = 5050;
const wait = (milliseconds) =>
    new Promise((res, rej) => {
        setTimeout(() => {
            res();
        }, milliseconds);
    });

const middleware1 = async (req, res, next) => {
    console.log('- 1');
    await wait(10);
    console.log('- 2');
    next();
};

const middleware2 = async (req, res, next) => {
    console.log('- 3');
    await wait(10);
    console.log('- 4');
    console.log('');
    next();
};

app.use(middleware1);
app.get('/', async (req, res, next) => {
    console.log('-- 1');
    await wait(10);
    console.log('-- 2');
    console.log('hello');
    res.send('Hello World!');
    console.log('-- 3');
    await wait(10);
    console.log('-- 4');
    next();
    return;
});
app.use(middleware2);

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});
您将中间件一分为二,以确保只有在执行路由中的所有内容之后才执行中间件2。您将得到如下输出:

- 1
- 2
-- 1
-- 2
hello
-- 3
-- 4
- 3
- 4

Express中没有用于
next()
的接口,无法知道所有其他请求处理程序何时完成
next()
本身基本上是异步的,因此您无法从它何时返回以及其他请求处理程序何时完成判断
next()
只是启动路由器中的下一个链接,它返回时不一定与路由完成时有任何关系。所以,你的问题不能这样做。哦,而且
next()
不会返回承诺,所以你不能使用
wait
来了解它何时完成。对于更以承诺为中心的设计,您可能希望查看或。Express中没有用于
next()
的接口,无法了解所有其他请求处理程序何时完成
next()
本身基本上是异步的,因此您无法从它何时返回以及其他请求处理程序何时完成判断
next()
只是启动路由器中的下一个链接,它返回时不一定与路由完成时有任何关系。所以,你的问题不能这样做。哦,而且
next()
不会返回承诺,所以你不能使用
wait
来了解它何时完成。对于更以承诺为中心的设计,您可能希望查看或。