Node.js – 静态文件服务器不工作

我正在试图在note.js中创build一个静态文件服务器,但它并没有真正的工作。

我的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"); 

当我现在尝试打开:localhost:3000 / index.html(索引文件是在文件夹WWW),我的浏览器只是加载和加载的东西,但不显示任何东西。 控制台中也没有错误。

我希望你可以帮助我!

JS

你是否试图用这个程序获得目录列表? 如果是,那么使用这个。

 var http = require('http'); var connect = require('connect'); var app = connect() .use('/public', connect.static(__dirname + '/public')) .use('/public', connect.directory(__dirname + '/public')) .use(function(req, res){ res.end('hello world\n'); }) http.createServer(app).listen(3000); // access it on http://localhost:3000/public 

我用过连接模块。 connect.directory提供目录列表, connect.static提供静态文件。

如果我没有正确理解你,请告诉我。

尝试这个 :

 var fs = require('fs'); var http = require('http'); var path = require("path"); var url = require("url"); var settings = require("../settings"); http.createServer(function (request, response) { var uri = url.parse(request.url).pathname; var filetype = (uri.split("/static/")[1]).split(".")[1]; var filename = path.join(process.cwd(), uri); fs.readFile(filename, function (error, content) { if (error) { response.writeHead(500); response.end(); } else { response.writeHead(200, { 'Content-Type': settings.STATIC.HEADERS[filetype.toUpperCase()] }); response.end(content, 'utf-8'); } }); }).listen(8000); 

添加一个像这样的设置文件:

 module.exports = { DOMAIN: "localhost", // Runnimg Domain , Production Only PORT: 8080, // Port Project Running On PROJECT: __dirname, // project directory DEFAULT_THEME: "default", // Default Theme for site DB: { // Database Information and Credintials HOST: "localhost", USER: 'root', PASSWORD: 'root', DATABASE: 'sockets' }, STATIC: { // Static Files and forlders information EXTENSIONS: ['psd', 'docs', 'zip', 'rar', 'png'], // allowed to download extensions HEADERS: {// MIME Types for diffrent types PDF: 'application/pdf', CSS: 'text/css', JS: 'text/javascript', HTML: 'text/html', TXT: 'text/plain', JPG: 'image/jpeg', JPEG: 'image/jpeg', GIF: 'image/gif ', DOWNLOAD: 'application/octet-stream' } } }; 

我总是喜欢把这个文件放在我将要使用的任何地方。

正如你所看到的代码等待这样的url:

 http://domain:port/static/file.ext 

你可以改变这一行:

 var filetype = (uri.split("/static/")[1]).split(".")[1]; 

希望这可以帮助