2014-01-22 46 views
1

我試圖在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

回答

1

您是否試圖使用此程序獲取目錄列表?如果是,那麼使用這個。

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服務於靜態文件。

如果我沒有正確理解你,請告訴我。

1

試試這個:

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' 
     } 
    } 
}; 

我總是喜歡和我將使用一切隨處可見這個文件。

,正如你可以看到代碼等待這樣一個網址:

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

可以更改該行:

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

希望這有助於

相關問題