Mohamed Swalih
Mohamed Swalih

Reputation: 1

node:_http_outgoing:862 issue in node js... fs not working

  1. the terminal is showing >>>

node:_http_outgoing:862 throw new ERR_INVALID_ARG_TYPE( ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined at new NodeError (node:internal/errors:399:5) at write_ (node:_http_outgoing:862:11) at ServerResponse.write (node:_http_outgoing:827:15) at ReadFileContext.callback (c:\Users\me\Documents\Web Devolopment Challenge\intro\sample-server.js:7:9) at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:324:13) { code: 'ERR_INVALID_ARG_TYPE' }

Node.js v18.14.1

this is my code

var http = require('http')
var fs = require('fs')
http.createServer(function (req, res) {

  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(7000);

and this is my html code

<html>
    <h1>hello guys</h1>
    <h2>heyy</h2>
</html>

Upvotes: 0

Views: 827

Answers (2)

Somesh Thakur
Somesh Thakur

Reputation: 1

Specify the encoding in "readFile" method as "utf8", it should work.

fs.readFile("demofile.html","utf8",function(err,data ...

Upvotes: 0

res.write(data) -- problem lies here in this data(arg).If you are getting this data from any function,then you may have forgotten to return the fun res.In my case I missed it,so I was getting the same error.So,check it and do the needful.Hope it works.

Upvotes: 0

Related Questions