Node.js 将使用以下代码创建文件但不会写入文件。 Windows XP 上的 node-186364e.exe
var
http = require('http'),
fs = require('fs'),
buffer,
working = 'notworking',
options = {
host: '202.138.228.141',
port: 8080,
path: 'www.google.com'
};
http.get(options, function(res) {
if (res.statusCode < 400) working = 'working';
writeResults(options);
}).on('error', function (e) {
writeResults(options);
});
function writeResults (options) {
buffer = '\n' + options.host + ':' + options.port + ':' + working;
fs.open('results.ht', 'a', function (err, fd) {
fs.write(fd, buffer, function (e, written, buf) {
fs.closeSync(fd);
});
});
}
var
http = require('http'),
fs = require('fs'),
buffer,
working = 'notworking',
options = {
host: '202.138.228.141',
port: 8080,
path: 'www.google.com'
};
http.get(options, function(res) {
if (res.statusCode < 400) working = 'working';
writeResults(options);
}).on('error', function (e) {
writeResults(options);
});
function writeResults (options) {
buffer = '\n' + options.host + ':' + options.port + ':' + working;
fs.open('results.ht', 'a', function (err, fd) {
fs.write(fd, buffer, function (e, written, buf) {
fs.closeSync(fd);
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想从 http.get 写入数据,您可以这样做:
然后将 writeResults 更改为:
If you want to write the data from the http.get, you'd do:
then maybe change writeResults to: