我想在我的控制檯中看到「有迴應」或「有錯誤」。NodeJS'http'對象應該有一個名爲get()的方法,但它在哪裏?
我一直在嘗試使用http.get()執行HTTP請求,但是當我嘗試時出現以下錯誤。
D:\wamp\www\Chat\server\test.js:19
http.get("http://google.com", function(res) {
^
TypeError: Object #<Server> has no method 'get'
at Object.<anonymous> (D:\wamp\www\Chat\server\test.js:19:6)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
這裏是全部test.js:
var http = require('http').createServer(handler);
var fs = require('fs');
http.listen(9090);
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
http.get("http://google.com", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
在cmd中返回執行node --version
v0.8.15
就是這樣。謝謝! – Joncom