简体   繁体   English

遇到node.js TypeError:第一个参数必须是字符串或具有尽可能简单脚本的Buffer

[英]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: 我建议:

  1. Try placing the 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相同的路径中,然后重试
  2. If you need them on subfolders, try navigating with absolute paths, or use some npm extension like rootpath 如果您需要在子文件夹中使用它们,请尝试使用绝对路径导航,或使用一些npm扩展名,例如rootpath
  3. In the fs callback, try logging data, it is also a good practice to use existsSync() to be sure that your app doesn't crash 在fs回调中,尝试记录数据,这是一个很好的做法,使用existSync()确保您的应用不会崩溃

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js和PostgreSQL查询-抛出新的TypeError(&#39;第一个参数必须是字符串或Buffer&#39;); - node.js and PostgreSQL query - throw new TypeError('first argument must be a string or Buffer'); node.js请求POST数组“第一个参数必须是字符串或缓冲区” - node.js request POST array “first argument must be string or buffer” “第一个参数必须是字符串或缓冲区” - 遵循 w3schools 的 Node.js 教程时出错 - "First argument must be a string or Buffer"- error when following w3schools' Node.js tutorial Node.js TypeError:path必须是字符串或Buffer - Node.js TypeError: path must be a string or Buffer 如何避免第一个参数必须是字符串或缓冲区节点js - How avoid first argument must be a string or Buffer node js TypeError:第一个参数必须是字符串或Buffer。 Java脚本 - TypeError: First argument must be a string or Buffer. Javascript 未处理的Promise拒绝警告:TypeError:第一个参数必须是字符串或缓冲区 - Unhandled Promise rejection warning: TypeError: First argument must be a string or Buffer 抛出信息 TypeError:第一个参数必须是字符串或Buffer - throw message; TypeError: first argument must be a string or Buffer TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型 Node.JS - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string Node.JS 500 TypeError:在express.js,node.js中不是字符串或缓冲区 - 500 TypeError: Not a string or buffer in express.js,node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM