2011-12-14 31 views
1

我在cloud9 IDE中進行了以下設置。Cloud9:未呈現nodejs服務器映像(HTML)

項目根文件夾

  1. 的Hello.html - 包含簡單html標籤(+圖像標籤)預覽顯示圖像
  2. HelloHtml.js - 節點JS文件,讀取HTML文件,並寫入到客戶(迴應)。 。
  3. Penguins.jpg - 圖像文件在同一個文件夾中。

當我運行服務並在瀏覽器中點擊URL時,HTML會以「Hello World!」呈現。被顯示爲。但圖像沒有被渲染。 img標籤中應該是src =「」屬性。

圖像文件的路徑應該是什麼?謝謝。

HelloHtml.js

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

http.createServer(function(request, response) { 
    response.writeHead(200, { 
     'Content-Type': 'text/html' 
    }); 
    fs.readFile('./Hello.html', function(err, data){ 
      if(err) throw err; 
      response.end(data); 
     }); 
}).listen(process.env.PORT); 
console.log('Hello World HTML Service has started.'); 

的Hello.html

<html> 
    <head> 
     <title>Node JS</title> 
    </head> 
    <body> 
     <h2>Hello world!</h2> 
     <img src="Penguins.jpg" /> 
    </body> 
</html> 

回答

1

你不處理服務於你的代碼的任何地方,你只是服務於文件中的靜態文件「hello.html的'不管是什麼:

http.createServer(function(request, response) { 
    response.writeHead(200, { 
     'Content-Type': 'text/html' 
    }); 
    fs.readFile('./Hello.html', function(err, data){ 
      if(err) throw err; 
      response.end(data); 
     }); 
}).listen(process.env.PORT); 

要麼做一個路由方案,基於請求URL,或者使用一些靜態文件服務器從這裏:

https://github.com/joyent/node/wiki/modules#wiki-web-frameworks-static

我建議你看看快車,它有和路由處理也:expressjs.com