node.js - 如何使用 Nodejs 在 post 请求之间传输数据

标签 node.js

我必须将数据从一个发布请求发送到同一服务器中的另一个发布请求,那么我如何将结果信息从 app.post('/convert') 发送到 app.post('/last') ?

这是我的帖子请求:

   app.post('/convert',  upload.single('file'), (req, res) => {

                let var1='asma'; 
       let  var2='hello ';

       let results={

        first:var1,

       others:var2
       }


        })


       })


app.post('/last', upload.single('file'),(req, res) =>{
        console.log(var1);
        console.log(var2);
        res.send(results);
 });

如何在第二个 app.post 中使用 var1 和 var2 ?

最佳答案

我假设您实际上不想发出新的后请求,只是想知道如何创建和使用中间件。

function convert(req,res,next){
  req.var1 = "hello";
  req.var2 = "world";
  next();
};

function last(req,res){
    console.log(req.var1);
    console.log(req.var2);
    res.send(results);
}

app.post('/doPlenty', [upload.single('file'), convert], last});

我是不是想太多了?

关于node.js - 如何使用 Nodejs 在 post 请求之间传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990671/

相关文章:

javascript - Puppeteer 获取有关页面加载的信息 - 加载的文件列表及其大小

node.js - Angular Route 在浏览器刷新时显示原始 JSON

javascript - Express Node 服务器与其显示 html 之间的通信

node.js - 字谜数组程序和数组组结果

node.js - Node.js 事件循环是如何工作的?

node.js - 我们如何将消息从特定事件中心分区获取到 azure 函数中,以及如何自动扩展 azure 函数的数量?

node.js - 在本地使用 NPM 依赖项

javascript - 如何使用 Node JS 打印整个 mongoDB 集合

node.js - Azure Web应用程序中lwip的node-gyp重建问题: fatal error C1083: Cannot open include file: 'nan.h' : No such file or directory

javascript - sails js : How can I define two MySQL connections to two different databases in a single model in Sails js?