Nodejs Api在后台调用(Nodejs Api call in background)

编程入门 行业动态 更新时间:2024-10-25 08:21:03
Nodejs Api在后台调用(Nodejs Api call in background)

我正在开发一个带有节点JS的应用程序,该应用程序会生成一个调用端点api.example.com/generate-report的报告

但是这个报告需要大约1分钟的时间才能生成,然后我想实现这样的事情:

用户点击生成报告 系统返回响应{生成:“ok”} 系统生成报告后发送一个通知(我知道该怎么办) 用户获得报告

这可能与nodejs?

I'm developing an app with node JS, the app generates a report calling the endpoint api.example.com/generate-report

But this report takes around 1 minute on be generated, then I want to implement something like this:

User click on generate report System return response {generating:"ok"} After the system generate the report send a notification (this I what I know how to do) User get the report

Is this possible with nodejs?

最满意答案

在我做了一些研究之后,可以使用Promise轻松完成。

要运行以下代码,需要安装express和node uuid

npm install --save express npm install --save uuid node index.js

索引的源代码是:

//index.js const express = require("express"); const app = express(); const PORT = process.env.PORT || 5000; const uuidV1 = require('uuid/v1'); // this is where we'll store the results of our jobs by uuid once they're done const JOBS = {}; app.get("/", (req, res) => { res.send("It works!"); }); app.get("/startjob", (req, res) => { let times = [100, 1000, 10000, 20000]; let promises = []; for (let time of times) { promises.push(new Promise((resolve, reject) => { setTimeout(resolve, time, `${time} is done.`); })); } // obviously, you'd want to generate a real uuid here to avoid collisions let uuid = uuidV1(); console.log(uuid); Promise.all(promises).then(values => { JOBS[uuid] = values; }); res.redirect(`progress/${uuid}`); }); app.get("/progress/:uuid", (req, res) => { if (JOBS[req.params.uuid] === undefined) { res.send("Still processing your request."); } else { res.send(`Here's your result: ${JOBS[req.params.uuid]}.`); // instead of immediately deleting the result of the job (and making it impossible for the user // to fetch it a second time if they e.g. accidentally cancel the download), it would be better // to run a periodic cleanup task on `JOBS` delete JOBS[req.params.uuid]; } }); app.listen(PORT, () => { console.log(`Listening on localhost:${PORT}.`); });

代码运行时,您将被重定向到/ process / uuid,并获得进程的状态。

这需要一些改进,因为我想要“{process:uuid}”这样的响应,并且我可以将其存储在我的本地存储上以供后续使用。

那么,我希望这对某个人有帮助。

After I do some research, this can be easily done using Promises.

To run the following code it's necessary to install express and node uuid

npm install --save express npm install --save uuid node index.js

The source code of index is:

//index.js const express = require("express"); const app = express(); const PORT = process.env.PORT || 5000; const uuidV1 = require('uuid/v1'); // this is where we'll store the results of our jobs by uuid once they're done const JOBS = {}; app.get("/", (req, res) => { res.send("It works!"); }); app.get("/startjob", (req, res) => { let times = [100, 1000, 10000, 20000]; let promises = []; for (let time of times) { promises.push(new Promise((resolve, reject) => { setTimeout(resolve, time, `${time} is done.`); })); } // obviously, you'd want to generate a real uuid here to avoid collisions let uuid = uuidV1(); console.log(uuid); Promise.all(promises).then(values => { JOBS[uuid] = values; }); res.redirect(`progress/${uuid}`); }); app.get("/progress/:uuid", (req, res) => { if (JOBS[req.params.uuid] === undefined) { res.send("Still processing your request."); } else { res.send(`Here's your result: ${JOBS[req.params.uuid]}.`); // instead of immediately deleting the result of the job (and making it impossible for the user // to fetch it a second time if they e.g. accidentally cancel the download), it would be better // to run a periodic cleanup task on `JOBS` delete JOBS[req.params.uuid]; } }); app.listen(PORT, () => { console.log(`Listening on localhost:${PORT}.`); });

When the code runs you will be redirected to /process/uuid and I get the status of the process.

This needs some improvements because I want the response like "{process:uuid}" and I can store this on my Local Storage to use after.

Well, I hope this help to someone.

更多推荐

本文发布于:2023-04-27 14:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   Api   Nodejs   call   background

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!