setInterval in nodejs
setInterval in nodejs
我正在发出一个http请求,该请求应该在每分钟运行一次。下面是我的代码
var express = require("express");
var app = express();
var recursive = function () {
app.get('/', function (req, res) {
console.log(req);
//Some other function call in callabck
res.send('hello world');
});
app.listen(8000);
setTimeout(recursive, 100000);
}
recursive();
根据上面的代码,我必须在每分钟后得到一次响应。但是我得到错误:听 EADDRINUSE。这方面的任何帮助都将非常有帮助。
此代码每分钟发出一次 http 请求:
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/'
};
function request() {
http.get(options, function(res){
res.on('data', function(chunk){
console.log(chunk);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
}
setInterval(request, 60000);
抛出 EADDRINUSE 错误,因为您无法在同一端口中启动服务器两次
它将以以下方式工作:
var express = require("express"),
app = express(),
recursive = function (req, res) {
console.log(req);
//Some other function call in callabck
res.send('hello world');
setTimeout(recursive, 100000);
};
app.get('/', recursive);
app.listen(8000);
}
相关文章:
- Require in nodejs
- ReadFile in Base64 Nodejs
- What is the reason for cloning the listeners array in nodejs
- Schema.pre in Mongoose modules (NodeJS)
- linkify urls in templates-nodejs
- NodeJS put array in MongoDB doc.validate 不是一个函数
- setTimeout in nodejs - for loops
- 无法在 nodeJS:TypeError 中对用户进行身份验证:无法使用“in”运算符在 fs 中搜索“用户名”
- setInterval in nodejs
- setTimeout in nodejs
- removeListener in the Listener in nodejs
- Stream in NodeJS
- RabbitMQ no ack in nodejs
- NodeJS MSSQL WHERE IN准备的SQL语句
- res.send vs res.locals in nodejs
- cURL post request in Nodejs.
- Adding in Javascript (NodeJS)
- Github Post-Receive Hook in Nodejs
- DRY in Nodejs Express
- Faye Websocket in NodeJS