html - 云9 : nodejs server image not getting rendered (HTML)

标签 html image node.js cloud9-ide

我在 cloud9 IDE 中进行了以下设置。

项目根文件夹

  1. Hello.html - 包含简单的 html 标签(+图像标签)预览显示图像
  2. HelloHtml.js - 读取 html 文件并写入客户端(响应)的 Node js 文件。 .
  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>

最佳答案

您没有处理代码中任何位置的静态文件,无论如何,您只是提供文件“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

我建议你看看 Express,它也有这个功能和路由处理:expressjs.com

关于html - 云9 : nodejs server image not getting rendered (HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8504290/

相关文章:

node.js - 使用 gm 将图像转换为 png 并调整其大小

html - 没有 javascript 的 <li> 元素列表

javascript - 渐进式图像在 HTML5 Canvas 中的渐进渲染

C# WPF从exe文件夹加载图片

python - 使用 Python (GAE) 读取图像的颜色

WPF Image 在运行时动态更改图像源

javascript - Foundation 5.0.3 刚刚下载,顶栏粘滞不工作

javascript - 如何从 JSON 创建下拉菜单

javascript - Node.js 会继续支持旧版本的 Node 模块吗

node.js - 在 Azure 应用服务上安装 python——我可以使用哪些工具?