I am new to Node.js and stuck on an error which is irritating me from 2 days!
The JS looks like this:
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
console.log("A request was made of url : " + req.url);
res.writeHead(200, { 'Content-Type': 'text/html' });
var data = fs.createReadStream(__dirname, '/index.html', 'utf8');
data.pipe(res);
});
server.listen(4242);
console.log("Server is running.....");
And this is the HTML file:
<!DOCTYPE <html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index Website</title>
</head>
<body>
<h1>Some HTML</h1>
</body>
</html>
But I end up getting this error:
string_decoder.js:13
throw new Error(`Unknown encoding: ${enc}`);
^
Error: Unknown encoding: /index.html
at normalizeEncoding (string_decoder.js:13:11)
at new StringDecoder (string_decoder.js:22:19)
at new ReadableState (_stream_readable.js:99:20)
at ReadStream.Readable (_stream_readable.js:108:25)
at new ReadStream (fs.js:1907:12)
at Object.fs.createReadStream (fs.js:1885:10)
at Server.<anonymous> (G:\Docs\Node.js\server.js:7:19)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIning [as onIning] (_http_server.js:546:12)
Can someone tell me what went wrong, or point me in the right direction?
I am new to Node.js and stuck on an error which is irritating me from 2 days!
The JS looks like this:
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
console.log("A request was made of url : " + req.url);
res.writeHead(200, { 'Content-Type': 'text/html' });
var data = fs.createReadStream(__dirname, '/index.html', 'utf8');
data.pipe(res);
});
server.listen(4242);
console.log("Server is running.....");
And this is the HTML file:
<!DOCTYPE <html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index Website</title>
</head>
<body>
<h1>Some HTML</h1>
</body>
</html>
But I end up getting this error:
string_decoder.js:13
throw new Error(`Unknown encoding: ${enc}`);
^
Error: Unknown encoding: /index.html
at normalizeEncoding (string_decoder.js:13:11)
at new StringDecoder (string_decoder.js:22:19)
at new ReadableState (_stream_readable.js:99:20)
at ReadStream.Readable (_stream_readable.js:108:25)
at new ReadStream (fs.js:1907:12)
at Object.fs.createReadStream (fs.js:1885:10)
at Server.<anonymous> (G:\Docs\Node.js\server.js:7:19)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIning [as onIning] (_http_server.js:546:12)
Can someone tell me what went wrong, or point me in the right direction?
Share Improve this question edited Sep 22, 2017 at 18:59 towc 2,0342 gold badges23 silver badges37 bronze badges asked Sep 22, 2017 at 17:23 Pranshu DunejaPranshu Duneja 511 gold badge3 silver badges10 bronze badges 7-
You seem to use
fs.createReadStream
incorrectly. – str Commented Sep 22, 2017 at 17:27 - Can you explain the mistake please ? – Pranshu Duneja Commented Sep 22, 2017 at 17:30
-
fs.createReadStream
takes parametersfs.createReadStream(path[, options])
so for your case you should write it likefs.createReadStream(`${__dirname}/index.html`, {encoding: 'utf8'})
– Prasanna Commented Sep 22, 2017 at 17:47 - Thank You so much Sir ! That solved my problem and I am feeling so much relaxed now :) – Pranshu Duneja Commented Sep 22, 2017 at 17:57
- I will put a ment with this answer. Please mark it correct. – Prasanna Commented Sep 22, 2017 at 18:30
2 Answers
Reset to default 3fs.createReadStream
takes parameters
fs.createReadStream(path[, options])
so for your case you should write it like
fs.createReadStream(`${__dirname}/index.html`, {encoding: 'utf8'})
for the use __dirname you should;
first join the path
const indexPath = path.join(___dirname, "index.html");
then
const data = fs.createReadStream(indexPath, 'utf8');