1

私は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>
4

1 に答える 1

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

于 2011-12-14T12:36:40.493 に答える