Javascript NodeJS'http'对象应该有一个名为get()的方法,但它在哪里?

Javascript NodeJS'http'对象应该有一个名为get()的方法,但它在哪里?,javascript,node.js,http,http-request,Javascript,Node.js,Http,Http Request,我希望在控制台中看到得到响应或出错 我一直在尝试使用执行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

我希望在控制台中看到得到响应或出错

我一直在尝试使用执行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)
在cmd中执行node-version将返回在您创建的服务器上调用get的v0.8.15,而不是在http对象上:

var http = require('http').createServer(handler);
您的http应该是:

var http = require('http');
然后可以使用http.get

http模块实际上有一个顶级get方法,但是您的变量http是对http.Server实例的引用,而不是对模块本身的引用。服务器没有发出客户端请求的方法。将前几行更改为

var http = require('http');

var fs = require('fs');

http.createServer(handler).listen(9090);

您的问题是,您要求的是httpServer执行get,而不是http本身!如果您这样做,“获取”方法将起作用:

var http = require('http');

http.get("http://google.com", function(res) {
    console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
    console.log("Got error: " + e.message);
});
这不需要创建服务器

var http = require('http');

http.get("http://google.com", function(res) {
    console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
    console.log("Got error: " + e.message);
});