Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在将响应发送到节点js中的客户端后从setTimeOut执行代码是一种错误的做法吗?_Node.js_Rest_Express_Http_Settimeout - Fatal编程技术网

Node.js 在将响应发送到节点js中的客户端后从setTimeOut执行代码是一种错误的做法吗?

Node.js 在将响应发送到节点js中的客户端后从setTimeOut执行代码是一种错误的做法吗?,node.js,rest,express,http,settimeout,Node.js,Rest,Express,Http,Settimeout,我想在响应发送到客户端后调用setTimeOut 对于在3分钟后更改数据库中的值,有什么问题吗 app.get(“/route”,(请求、恢复、下一步)=>{ //以db为单位更新值 //然后在响应中发送值 res.status(200).json({newValue:value}).end(); 设置超时(()=>{ //3分钟后更改数据库中的值 },180000) })使用setTimeout进行某些工作没有问题,但这是一个简单的解决方案。如果服务器停机,任务将丢失。因此,更好的方法是使用

我想在响应发送到客户端后调用setTimeOut 对于在3分钟后更改数据库中的值,有什么问题吗

app.get(“/route”,(请求、恢复、下一步)=>{
//以db为单位更新值
//然后在响应中发送值
res.status(200).json({newValue:value}).end();
设置超时(()=>{
//3分钟后更改数据库中的值
},180000)

})
使用
setTimeout
进行某些工作没有问题,但这是一个简单的解决方案。如果服务器停机,任务将丢失。因此,更好的方法是使用类似的调度器,它将在每分钟轮询数据库以查看是否有任何行符合条件。如果将应用程序更改为这种体系结构,则只需向表中添加一行,过期时间为
T+3分钟
,cron作业将检查表中是否有未处理的条目,并对这些数据执行操作

var cron = require('node-cron');

cron.schedule('* * * * *', () => {
  console.log('running a task every minute');
  /* Get entries from database table which has a timestamp < now and is unprocessed and act on those data */
 
 // do the processing

 // mark the row as processed
});

谢谢,我一定会看这个模块,您知道它在MongoDB中是否也能正常工作吗?是的,它在MongoDB中也能正常工作,事实上,它是一种不依赖数据库的方法。在MongoDB中,通过设置
TTL
,文档可以自动过期。看到这个了吗?是的,我熟悉TTL,我用它来清除会话,问题是我的SMS代码不是一个文档,而是另一个文档中的一个字段,我想我会尝试node cron,看看我在这里为我的问题添加了更多信息,我想执行的任务是在MongoDB中插入一些随机代码,用于SMS代码,但我想在3分钟后更改该值,mongo中是否有任何功能可以做到这一点?或者,我的最佳解决方案是在节点服务器中执行此操作,如果我仍然按照上面的原始方式执行此操作,那么如果我获得大量流量,是否会导致其他客户端对服务器的调用出现问题?
app.get("/route",(req,res,next) => {

// update value in db
// then send the value in the response
res.status(200).json({newValue : value}).end();

// write a row to database table with expiry now() + 3 minutes

})