我正在开发一个带有节点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 reportIs 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.jsThe 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.
更多推荐
发布评论