2011-12-14 26 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