0

This is the code of my nodejs file. How can I solve the issue? Below I also posted the picture of the error I am facing.

const http = require('http');

const fs = require('fs');

var requests = require('requests');

const homefile = fs.readFileSync('index.html', 'utf8');

// console.log(homefile);

const replaceVal = (tempVal, orgVal) => {

let temper = tempVal.replace("{%temp%}", orgVal.main.temp);

temper = temper.replace("{%mintemp%}", orgVal.main.temp_min);

temper = temper.replace("{%maxtemp%}", orgVal.main.temp_max);

temper = temper.replace("{%location%}", orgVal.name);

temper = temper.replace("{%country%}", orgVal.sys.country);}

  const server = http.createServer( (req, res) => {
            if(req.url == '/'){
    requests("https://api.openweathermap.org/data/2.5/weather?q=silchar&units=metric&appid=").on("data", (chunk)=>{
        const objdata = JSON.parse(chunk);
        // console.log(objdata);
        const arrData = [objdata];
        // console.log(arrData)
        const realData = arrData.map((val) => replaceVal(homefile, val));
    
        res.write(realData);
        console.log(realData);
    }).on("end", (err)=>{
        if(err) return console.log("Connection closed due to error: " + err);
        res.end();
    })
    }else{
        res.end('404: File Not Found');
    }
})

server.listen(8000,'127.0.0.1');

I am facing this problem.. This is the ERROR:

http_outgoing.js:722
    throw new ERR_INVALID_ARG_TYPE('first argument',
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array
    at write_ (_http_outgoing.js:722:11)
    at ServerResponse.write (_http_outgoing.js:687:15)
    at bobthebuilder.<anonymous> (C:\xampp\htdocs\Html,css,JS,php\WeatherAPI\index.js:26:13)
    at bobthebuilder.emit (C:\xampp\htdocs\Html,css,JS,php\WeatherAPI\node_modules\eventemitter3\index.js:181:35)
    at bobthebuilder.stream (C:\xampp\htdocs\Html,css,JS,php\WeatherAPI\node_modules\requests\index.js:90:16)
    at bobthebuilder.emit (C:\xampp\htdocs\Html,css,JS,php\WeatherAPI\node_modules\eventemitter3\index.js:182:35)
    at load (C:\xampp\htdocs\Html,css,JS,php\WeatherAPI\node_modules\loads\index.js:136:10)

This is the error

4

2 Answers 2

0

Problem occurs here:

res.write(realData);

The realData variable is an Array instance, but the res.write() method requires a parameter of string or Buffer instance, you could transform the realData variable to check.

Sign up to request clarification or add additional context in comments.

Comments

0
const realData = arrData.map((val) => replaceVal(homeFile, val));
const joinedData = realData.join();
console.log(joinedData); 
res.write(joinedData);

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.