“类型错误:response.write 不是一个函数”错误

发布于 2025-01-15 05:18:49 字数 1727 浏览 3 评论 0原文

我正在尝试学习一些 Node.js,这是我的第一个教程项目。 虽然我改变了一些小东西,但我主要遵循这个教程。 https://www.youtube.com/watch?v=PFJHQ2g6s0k&ab_channel= itsTechMode

我的 JS 代码如下。

const http = require('http');
const url = 'https://api.openweathermap.org/data/2.5/weather?q=Athens,GR&appid=a7003b6a9783fdc3503466334cbd605b&units=metric';
let data;

var server = http.createServer(function(request,response){

  var request = require('request');

  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write("Hello write");
  response.end('Hello World\n');
  
  

  request({ url: url }, (error, response, body) => {
    if (error) {
        console.log(error)
    }

    let data = JSON.parse(body);

    response.write("Trying to print this.");
    response.end();
    

    // response.write("<html><body><div id='container'>");
    // response.write("<h1>" +"City Name - :"+ data['name']+"<br>"+"</h1>");
    // response.write("<h2>"+ "Temperature - :" + data.main['temp'] + "<br>"+"</h2>");
    // response.write("<h2>"+"Sunset Time - :"+ new Date(data.sys["sunset"]+1000)+ "</h2>");
    // response.write("</div></body></html>");
    

    console.log(response.body)
    response.end();
  });
    
}).listen(8000, "127.0.0.1");

我收到“TypeError:response.write 不是函数” Stackoverflow 不允许我添加图片。所以这里是错误屏幕截图的链接。 有人可以帮我吗?另外,如果这篇文章的格式看起来很奇怪,我很抱歉。我尽力让它看起来最好,但我是这里的新手。

I'm trying to learn some Node.js and this is my first tutorial project.
Although I changed some minor things, I mainly followed this tutorial.
https://www.youtube.com/watch?v=PFJHQ2g6s0k&ab_channel=ItsTechMode

My JS code is below.

const http = require('http');
const url = 'https://api.openweathermap.org/data/2.5/weather?q=Athens,GR&appid=a7003b6a9783fdc3503466334cbd605b&units=metric';
let data;

var server = http.createServer(function(request,response){

  var request = require('request');

  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write("Hello write");
  response.end('Hello World\n');
  
  

  request({ url: url }, (error, response, body) => {
    if (error) {
        console.log(error)
    }

    let data = JSON.parse(body);

    response.write("Trying to print this.");
    response.end();
    

    // response.write("<html><body><div id='container'>");
    // response.write("<h1>" +"City Name - :"+ data['name']+"<br>"+"</h1>");
    // response.write("<h2>"+ "Temperature - :" + data.main['temp'] + "<br>"+"</h2>");
    // response.write("<h2>"+"Sunset Time - :"+ new Date(data.sys["sunset"]+1000)+ "</h2>");
    // response.write("</div></body></html>");
    

    console.log(response.body)
    response.end();
  });
    
}).listen(8000, "127.0.0.1");

I get the "TypeError: response.write is not a function"
Stackoverflow does not allow me to add a pic. so here is a link to the error screenshot.
Can someone help me about it? Also, sorry if the this post's format looks weird. I tried to make it look as best as I could but I'm a newbie around here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-01-22 05:18:49

此行中的参数 response

var server = http.createServer(function(request,response){...}

是您需要用来执行“ response.write”,但是在这里:

request({ url: url }, (error, response, body) => {

您还有一些其他参数,您将其命名为“response”。这使得原始“响应”变量将

request(...) 调用中的参数名称更改为“resp”或其他名称,并确保在回调中使用“resp.body”。

The parameter response in this line:

var server = http.createServer(function(request,response){...}

Is what you need to use to do stuff like "response.write", however here:

request({ url: url }, (error, response, body) => {

You have some other parameter which you are naming "response". That makes the original "response" variable inaccessible.

Change the parameter name in the request(...) call to "resp" or something. And be sure to use "resp.body" within the callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文