NodeJs: The "chunk" argument must be of type string or an instance of Buffer

Dung Do Tien May 27 2022 649

Hi dev guys. I am a newbie in Nodejs. I wrote an application that help change the random background colors for this app. I created a module that help return a random color for me. Like this: 

colors.js

class Color {
    constructor(name, code) {
      this.name = name;
      this.code = code;
    }
}

const allColors = [
    new Color('brightred', '#E74C3C'),
    new Color('soothingpurple', '#9B59B6'),
    new Color('skyblue', '#5DADE2'),
    new Color('leafygreen', '#48C9B0'),
    new Color('sunkissedyellow', '#F4D03F'),
    new Color('groovygray', '#D7DBDD'),
];

exports.getRandomColor = () => {
    return allColors[Math.floor(Math.random() * allColors.length)];
}

exports.allColors = allColors;

index.js

const http = require('http');
const colors = require('./colors');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end(colors.getRandomColor());
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

But when I run the command node index.js I got an exception throw TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Color.

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Color
    at ServerResponse.end (_http_outgoing.js:745:13)
    at Server.<anonymous> (C:\Users\conta\source\repos\NodeJs\Demo1\index.js:9:7)
    at Server.emit (events.js:315:20)
    at parserOnIncoming (_http_server.js:790:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}

I tried to log console.log(colors.getRandomColor()); but it's still returning random colors for me well.

Anyone can help me?

Have 1 answer(s) found.
  • M

    Minh Yến May 27 2022

    This error occurred because your function colors.getRandomColor() return an object, not a string. So you need to convert it to string first. you can use JSON.stringify() method, see the code below:

    const http = require('http');
    const colors = require('./colors');
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end(JSON.stringify(colors.getRandomColor())); // Convert object to string here
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });

    It's will solved for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close