Node.js returns 给客户端的空白 html 文件
Node.js returns a blank html file to client
我是 node.js 的新手,找到了一个简单的教程来设置一个简单的 node.js 服务器。我正在尝试在客户端呈现一个 html 页面,我确实得到了我可以在控制台中看到的文件,但是浏览器显示一个空页面?这是我的 server.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.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("Oops, this doesn't exist! - 404");
}
else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
console.log('Connection working!');
}
});
break;
default:
response.writeHead(404);
response.write("Oops, this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
这是我的简单 html 文件 socket.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<h1>This is the socket.html file</h1>
</div>
</body>
</html>
我在浏览器中的回复:
我仔细检查了我使用的 'url' 和 'fs' 模块是否正确,我认为我是正确的?有人可以帮忙吗?
fs.readfile
是异步的,将在您调用 response.end()
.
后 return 文件内容
这是一个快速修复方法。希望它有效。
我将 response.end()
移到了 fs.readFile
回调中。
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');
response.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("Oops, this doesn't exist! - 404");
}
else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
console.log('Connection working!');
}
response.end();
});
break;
default:
response.writeHead(404);
response.write("Oops, this doesn't exist - 404");
response.end();
break;
}
});
server.listen(8001);
我是 node.js 的新手,找到了一个简单的教程来设置一个简单的 node.js 服务器。我正在尝试在客户端呈现一个 html 页面,我确实得到了我可以在控制台中看到的文件,但是浏览器显示一个空页面?这是我的 server.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.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("Oops, this doesn't exist! - 404");
}
else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
console.log('Connection working!');
}
});
break;
default:
response.writeHead(404);
response.write("Oops, this doesn't exist - 404");
break;
}
response.end();
});
server.listen(8001);
这是我的简单 html 文件 socket.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<h1>This is the socket.html file</h1>
</div>
</body>
</html>
我在浏览器中的回复:
我仔细检查了我使用的 'url' 和 'fs' 模块是否正确,我认为我是正确的?有人可以帮忙吗?
fs.readfile
是异步的,将在您调用 response.end()
.
这是一个快速修复方法。希望它有效。
我将 response.end()
移到了 fs.readFile
回调中。
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');
response.end();
break;
case '/socket.html':
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("Oops, this doesn't exist! - 404");
}
else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
console.log('Connection working!');
}
response.end();
});
break;
default:
response.writeHead(404);
response.write("Oops, this doesn't exist - 404");
response.end();
break;
}
});
server.listen(8001);