2013-02-16 11 views
0

我是節點js的新手。我正在關注該鏈接。但是,這始終執行response.writeHead(404);所以我不能夠查看的index.html http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js基本Web服務器與Node Js發行

這是我的web服務器節點的js代碼。

var http = require('http'); 
var path = require('path'); 
var fs = require('fs'); 

http.createServer(function (request, response) { 

    console.log('request starting...'); 

    var filePath = "."+request.url; 
    if (filePath == './') 
     filePath = '/index.html'; 
    else 
     console.log(request.url); 
    var extname = path.extname(filePath); 
    var contentType = 'text/html'; 
    switch (extname) { 
     case '.js': 
      contentType = 'text/javascript'; 
      break; 
     case '.css': 
      contentType = 'text/css'; 
      break; 
    } 

    fs.exists(filePath, function (exists) { 
     console.log(filePath); 
     if (exists) { 

      fs.readFile(filePath, function (error, content) { 
       if (error) { 
        console.log(error); 
        response.writeHead(500); 
        response.end(); 
       } 
       else { 
        response.writeHead(200, { 'Content-Type': 'text/html' }); 
        response.end(content, 'utf-8'); 
       } 
      }); 
     } 
     else { 
      response.writeHead(404); 
      response.end(); 
     } 
    }); 

}).listen(8125); 

我的問題。

  1. 這不顯示index.html。
  2. 如何發送基於不同請求的回覆或html文件? 例如:http://localhost:8125 - > index.html http://localhost:8125/firstpage.html - > firstpage.html
  3. 我們是否需要將html文件放在包含server.js(web服務器)的相同目錄中?

我也在cloud9 IDE中嘗試過。

enter image description here

我在SO發現了類似的問題。但我沒有清楚如何解決這個問題。 Node.js - Socket.io client file not being served by basic node web server 在此先感謝。

回答

1

1-是的,沒錯。我可以重現錯誤。我不熟悉教程,但顯然filePath = '/index.html';行應該是filePath = './index.html';

否則,服務器正在查找「/index.html」,即在文件系統根目錄下名爲「index.html」的文件,而不是在當前目錄中。也許它是在Windows操作系統上工作,但是在* nix上,這不起作用。我添加了.,index.html已送達。

2 - 爲此,您需要輸入「網址」的Node.js模塊,然後您可以訪問解析URL的便捷方式:http://nodejs.org/api/http.html#http_request_url 基本上,fs.exist ...前行,你可以添加以下內容:

var requestedfile = urlparser.parse(request.url).pathname;
console.log("requested URL path name : " + requestedfile);

,然後你就可以處理不同的請求。

3-是的,你可以。請記住,添加點「」時。在問題1的「index.html」之前,您提供了index.html的相對路徑(相對於server.js位於文件系統中的位置)。因此,您可以用這種方式指定任何其他相對路徑。

如果您將頁面存儲在當前文件夾之外的其他目錄中(對於isntance/var/www/mysite),仍然可以通過將包含此絕對路徑的字符串變量與請求路徑名連接起來來提供這些頁面。 例如(雖然intested)

var pagestorage = "/var/www/mysite/"; 
(...) 
var pathname = urlparser.parse(request.url).pathname; 
var requestfile = pagestorage + pathname; 

以下是更新文件的完整samlpe:

var http = require('http'); 
var path = require('path'); 
var fs = require('fs'); 
var urlparser = require('url'); 

http.createServer(function (request, response) { 

    console.log('request starting...'); 

    var filePath = "."+request.url; 
    if (filePath == './') 
     filePath = './index.html'; 
    else 
     console.log(request.url); 
    var extname = path.extname(filePath); 
    var contentType = 'text/html'; 
    switch (extname) { 
     case '.js': 
      contentType = 'text/javascript'; 
      break; 
     case '.css': 
      contentType = 'text/css'; 
      break; 
    } 

    // documentation at : http://nodejs.org/api/http.html#http_request_url 
    var requestedfile = urlparser.parse(request.url).pathname; 
    console.log("requested URL path name : " + requestedfile); 

    fs.exists(filePath, function (exists) { 
     console.log("looking up : " + filePath); 
     if (exists) { 

      fs.readFile(filePath, function (error, content) { 
       if (error) { 
        console.log("error" + error); 
        response.writeHead(500); 
        response.end(); 
       } 
       else { 
        response.writeHead(200, { 'Content-Type': 'text/html' }); 
        response.end(content, 'utf-8'); 
       } 
      }); 
     } 
     else { 
      response.writeHead(404); 
      response.end(); 
     } 
    }); 

}).listen(8125); 

HTH

相關問題