[英]running into node.js TypeError: First argument must be a string or Buffer with the simplest possible script
So I ran into an error reading TypeError: First argument must be a string or Buffer
when running a node.js script, and after a lot of stackoverflow and tutorial googling I couldn't find the solution, so I created sample code literally copy-pasted from the W3Schools node.js tutorial, which still returns the TypeError. 因此,我遇到了读取
TypeError: First argument must be a string or Buffer
的错误TypeError: First argument must be a string or Buffer
运行node.js脚本时, TypeError: First argument must be a string or Buffer
,经过大量的堆栈溢出和教程搜索之后,我找不到解决方案,因此我创建了示例代码,实际上是从W3Schools node.js教程粘贴,该教程仍返回TypeError。 The code in question is: 有问题的代码是:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return it's content:
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
demofile1.html: demofile1.html:
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
the console still returns the error 控制台仍然返回错误
TypeError: First argument must be a string or Buffer
at write_ (_http_outgoing.js:642:11)
at ServerResponse.write (_http_outgoing.js:617:10)
at ReadFileContext.callback (*file path*)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13)
I'm assuming that something has to be wrong with my environment, but I've installed node.js and run npm install fs manually, all to no avail. 我以为我的环境有问题,但是我已经安装了node.js并手动运行npm install fs,但都无济于事。 I can run other node.js servers fine, but the error comes when I try to read an html file using fs.
我可以很好地运行其他node.js服务器,但是当我尝试使用fs读取html文件时出现错误。
Thanks 谢谢
I don't recommend W3Schools as a respectable tutorial source, and this is a great example why not: they don't teach good habits like error handling. 我不推荐W3Schools作为值得推荐的教程源,这是为什么不这样做的一个很好的例子:他们不教错误处理等良好习惯。 Because you copied and pasted the example as they had it, your error is rather cryptic.
因为您按原样复制并粘贴了示例,所以您的错误相当隐晦。 However with good error handling, your error would be caught earlier and give you a much better indication of what went wrong.
但是,通过良好的错误处理,您的错误将更早被发现,并且可以更好地指示发生了什么错误。
With that in mind, try this: 考虑到这一点,请尝试以下操作:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return it's content:
fs.readFile('demofile1.html', function(err, data) {
if (err) throw err; // crash with actual error instead of assuming success
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
It looks that demofile1.html
is not found on the same path as where you are starting you server. 看起来在与服务器启动路径相同的路径上找不到
demofile1.html
。
I would recommend to: 我建议:
server.js
(or whatever you named your js file) in the same path as your demofile1.html
and try again server.js
(或您命名为js文件的任何文件)放置在与demofile1.html
相同的路径中,然后重试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.