【问题标题】:Cloud9: nodejs server image not getting rendered (HTML)Cloud9:nodejs 服务器图像未呈现(HTML)
【发布时间】:2012-01-20 05:11:27
【问题描述】:

我在 cloud9 IDE 中有以下设置。

项目根目录

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

【问题讨论】:

    标签: html image node.js cloud9-ide


    【解决方案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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 2016-04-03
      • 2014-08-22
      相关资源
      最近更新 更多