我试图在note.js中创建一个静态文件服务器,但它并不真正工作。Node.js - 静态文件服务器不工作
我fileserver.js代码:
var http = require("http"),
sys = require("sys"),
path = require("path"),
url = require("url"),
filesys = require("fs"),
rootpath = "D:\\NodeJS\\WWW\\",
port = 3000;
http.createServer(function(request,response){
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),rootpath+my_path);
path.exists(full_path,function(exists){
if(!exists){
response.writeHeader(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
}
else{
filesys.readFile(full_path, "binary", function(err, file) {
if(err) {
response.writeHeader(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
}
else{
response.writeHeader(200);
response.write(file, "binary");
response.end();
}
});
});
})
http.createServer(function(request,response){
var my_path = url.parse(request.url).pathname;
filesys.readFile(rootpath + my_path,response);
}).listen(port);
sys.puts("Server Running on 3000");
现在当我尝试打开:本地主机:3000/index.html的(索引文件是在文件夹WWW),我的网页浏览器只是载荷和载荷的东西但没有显示任何东西。控制台中也没有错误。
我希望你能帮助我!
JS