我正在尝试运行一个简单的应用程序,使用http服务器模块检查URL的状态.
基本上这是简单的http服务器:
require('http').createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('URL is OK');
}).listen(4000);
Run Code Online (Sandbox Code Playgroud)
现在,我希望使用此部分检查URL的状态:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("URL is OK") // Print the google web page.
}
})
Run Code Online (Sandbox Code Playgroud)
所以基本上我想启动节点,打开一个浏览器并显示内容,文字说"URL正常".然后每隔10分钟刷新一次.
任何帮助是极大的赞赏.
Pla*_*ato 11
使用node的一般策略是,您必须在回调中放置任何取决于异步操作结果的内容.在这种情况下,这意味着等待发送您的回复,直到您知道Google是否已启动.
要每10分钟刷新一次,您需要在所服务的页面中编写一些代码,可能使用<meta http-equiv="refresh" content="30">(30s),或使用首选方法的一种 javascript技术来重新加载JavaScript页面?
var request = require('request');
function handler(req, res) {
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("URL is OK") // Print the google web page.
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('URL is OK');
} else {
res.writeHead(500, {'Content-Type': 'text/html'});
res.end('URL broke:'+JSON.stringify(response, null, 2));
}
})
};
require('http').createServer(handler).listen(4000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13545 次 |
| 最近记录: |