Node.js: TypeError: object is not a function

前端 未结 2 1869
无人及你
无人及你 2021-02-07 04:53

I have a weird error:

var http = require(\"http\");
var request = require(\"request\");

http.createServer(function(request, response) {
    response.writeHead(2         


        
相关标签:
2条回答
  • 2021-02-07 05:13

    You are no longer able to access the global variable 'request'. You need to rename your local variable 'request' with some other name and the problem will be resolved.

    0 讨论(0)
  • 2021-02-07 05:16

    Access to the request global variable is lost as you have a local variable with the same name. Renaming either one of the variables will solve this issue:

    var http = require("http"); var request = require("request");
    
    http.createServer(function(req, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
        request('http://www.google.com', function (error, response, body) {
          if (!error && response.statusCode == 200) {
            console.log(body) // Print the google web page.
          }
        })
      response.end();
    }).listen(8888);
    
    0 讨论(0)
提交回复
热议问题