Javascript NodeJS抛出新类型错误
当我尝试运行JS文件时,出现以下错误:Javascript NodeJS抛出新类型错误,javascript,node.js,Javascript,Node.js,当我尝试运行JS文件时,出现以下错误: http.js:783 throw new TypeError('first argument must be a string or Buffer'); 我正在学习这个教程,它似乎没有提到这个问题 我的JS文件有: var http = require('http'), fs = require('fs'), sanitize = require('validator').sanitize; var app = http.cr
http.js:783
throw new TypeError('first argument must be a string or Buffer');
我正在学习这个教程,它似乎没有提到这个问题
我的JS文件有:
var http = require('http'),
fs = require('fs'),
sanitize = require('validator').sanitize;
var app = http.createServer(function (request, response) {
fs.readFile("client.html", 'utf-8', function (error, data) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
response.end();
});
}).listen(1337);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('message_to_server', function(data) {
var escaped_message = sanitize(data["message"]).escape();
io.sockets.emit("message_to_client",{ message: escaped_message });
});
});
我在node_modules文件夹中安装了Socket.io和validator。我对这类东西很陌生,看起来这篇教程不是一个好的开始选择,我似乎无法让它工作 您没有进行任何错误检查,我敢打赌
readFile
正在抛出错误。这意味着数据
未定义,因此当您尝试响应.写入(数据)
时,http模块会抛出一个错误
始终检查回调函数中的错误参数,并进行适当处理
fs.readFile("client.html", 'utf-8', function (error, data) {
if (error) {
response.writeHead(500, {'Content-Type': 'text/html'});
response.write(error.toString());
} else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
}
response.end();
});
@tymeJV希望我知道-我找不到文件所在的节点文件夹!啊,我现在遇到了一个更有用的错误
error:enoint,打开'client.html'
@Dave:enoint
意味着找不到文件。我知道,但看到这里:它肯定在那里=/Node可能没有按照您的预期解析路径。路径是相对于当前工作目录解析的,而不是脚本文件所在的位置。尝试使用--\uuu dirname+'/client.html'
我已经更改了它,不确定它是否有效,因为当我再次运行js文件时,我遇到了错误:listen EADDRINUSE
,我不知道如何释放端口以再次运行它=/