node.js 错误 - throw new TypeError('第一个参数必须是字符串或缓冲区');

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10470133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:40:07  来源:igfitidea点击:

node.js Error - throw new TypeError('first argument must be a string or Buffer');

node.js

提问by Krish

I'm trying to implement a basic addition program in node.js that accepts 2 numbers through the URL (GET Request) adds them together, and gives the result.

我正在尝试在 node.js 中实现一个基本的加法程序,它通过 URL(GET 请求)接受 2 个数字,将它们加在一起,并给出结果。

    var http = require("http");
    var url1  = require("url");

    http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      var path = url1.parse(request.url).pathname;

      if(path == "/addition")
      {
        console.log("Request for add recieved\n");

        var urlObj = url1.parse(request.url, true);

        var number1 =  urlObj.query["var"]; 
        var number2 =  urlObj.query["var2"];
        var num3 = parseInt(number2);
        var num4 = parseInt(number1);

        var tot = num3 + num4;

        response.write(tot);
        response.write(number1 + number2);

      }
      else
      {
        response.write("Invalid Request\n");              
      }
      response.end();

    }).listen(8889);

      console.log("Server started.");

When I run, I'm getting 'Server started' message in the console. But when i request the url

当我运行时,我在控制台中收到“服务器已启动”消息。但是当我请求网址时

`http://localhost:8889/addition?var=1&var2=20`

I'm getting the following error :

我收到以下错误:

http.js:593 throw new TypeError('first argument must be a string or Buffer');

http.js:593 throw new TypeError('第一个参数必须是字符串或缓冲区');

When I comment out the line that displays the variable 'tot', the code is running, and the output I get is the concatenated value of the 2 get parameters I pass. In this case, it happens to be 1+20 = 120. I'm not able to convert the data into numerical format.

当我注释掉显示变量 'tot' 的行时,代码正在运行,我得到的输出是我传递的 2 个 get 参数的串联值。在这种情况下,它恰好是 1+20 = 120。我无法将数据转换为数字格式。

Where is the mistake in the code? And what does the error message basically mean?

代码错误在哪里?错误信息基本上是什么意思?

Many thanks in advance.

提前谢谢了。

回答by jjrv

You're passing numbers to response.write, when they should be strings. Like this:

当它们应该是字符串时,您将数字传递给 response.write。像这样:

response.write(total + '');

The variable total contains the number 21 because you passed the query parameters through parseInt() before summing. It will cause an error when sent through response.write unless you convert to a string first, by appending the empty string to it. number1+number2 is OK because they're strings, but their "sum" is "120".

变量 total 包含数字 21,因为您在求和之前通过 parseInt() 传递了查询参数。通过 response.write 发送时会导致错误,除非您先将空字符串转换为字符串。number1+number2 没问题,因为它们是字符串,但它们的“总和”是“120”。

I'd suggest also looking into the node.js package "express". It handles a lot of the basics for a HTTP server so you can write like:

我建议还查看 node.js 包“express”。它处理 HTTP 服务器的许多基础知识,因此您可以这样编写:

var express=require('express');

var app=express.createServer();

app.get('/add',function(req,res) {
    var num1 = parseInt(req.query.var);
    var num2 = parseInt(req.query.var2);

    var total = num1 + num2;

    res.send(total + '');
});

app.listen(8888);