我得到一个 fs.js:347 throw new TypeError('Bad arguments');
I'm getting a fs.js:347 throw new TypeError('Bad arguments');
我试图按照有关使用 Node.js 创建路由器的教程进行操作。这是路由器的代码..
var http = require("http");
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case '/socket.html':
fs.readFileSync(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
当我访问 http://localhost:8001/socket.html 时,我的浏览器出现空白屏幕,我的终端显示
Connection
fs.js:347
throw new TypeError('Bad arguments');
^
TypeError: Bad arguments
at Object.fs.readFileSync (fs.js:347:11)
at Server.<anonymous> (/Users/Vik/Desktop/NodeJS/routingToDifferentUrls.js:15:20)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:529:20)
然后退出。
谁能解释一下为什么我会收到这个错误???
是的,我已经创建了我的 socket.html 文件,它只显示简单的文本 "this is our socket.html file."
非常感谢您的帮助!
好吧,看起来您正在使用异步的 fs.readFile
。所以 response.end()
在 finishes.hence 你的错误消息 write after end
之前被调用。你真的想使用 fs.readFileSync 来获取 socket.html 的内容。喜欢 var data = fs.readFileSync.(__dirname + path)
我试图按照有关使用 Node.js 创建路由器的教程进行操作。这是路由器的代码..
var http = require("http");
var url = require('url');
var fs = require('fs');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case '/socket.html':
fs.readFileSync(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 404");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
当我访问 http://localhost:8001/socket.html 时,我的浏览器出现空白屏幕,我的终端显示
Connection
fs.js:347
throw new TypeError('Bad arguments');
^
TypeError: Bad arguments
at Object.fs.readFileSync (fs.js:347:11)
at Server.<anonymous> (/Users/Vik/Desktop/NodeJS/routingToDifferentUrls.js:15:20)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:529:20)
然后退出。
谁能解释一下为什么我会收到这个错误??? 是的,我已经创建了我的 socket.html 文件,它只显示简单的文本 "this is our socket.html file."
非常感谢您的帮助!
好吧,看起来您正在使用异步的 fs.readFile
。所以 response.end()
在 finishes.hence 你的错误消息 write after end
之前被调用。你真的想使用 fs.readFileSync 来获取 socket.html 的内容。喜欢 var data = fs.readFileSync.(__dirname + path)