Cloud9:未呈现nodejs服务器映像(HTML)
我在云9 IDE中有以下设置。
项目根文件夹
- Hello.html – 包含简单的html标签(+图片标签) 预览显示图片
- HelloHtml.js – 读取html文件并写入客户端(响应)的节点js文件。 。
- Penguins.jpg – 图片文件在同一个文件夹中。
当我运行服务并在浏览器中inputURL时,HTML会以“Hello World!”呈现。 被显示为。 但是图像没有被渲染。 img标签中应该是src =“”属性。
应该是什么图像文件的path? 谢谢。
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>
你不是在处理代码中的任何地方处理静态文件,而只是在服务文件“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或者从这里使用一些静态文件服务器来创build一个路由scheme:
https://github.com/joyent/node/wiki/modules#wiki-web-frameworks-static
我build议你看看Express,它也有这个和路由处理:expressjs.com