0

node.js を使用して内部に raphael.js スクリプトを含む HTML ページを提供しようとしています。

私のindex.htmlファイルは次のとおりです。

<!DOCTYPE html>
<head>
<title>Raphael  testing html</title>    
<script  src="raphael-min.js"></script>  
<script>
    // Initialize container when document is loaded
    window.onload = function () {
        paper = Raphael(0, 0, 640, 720, "container");
        paper.circle(100,100,50).attr('fill','red');
    };
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>

私のserver.jsファイルは次のとおりです。

var httpServer = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');

var httpServer = httpServer.createServer(function(request, response) {
 console.log((new Date()) + ' Received request for ' + request.url);
 response.writeHead(200, {'Content-Type': 'text/html'});
response.end(index);
return;
});

httpServer.listen(8080, function() {});

ブラウザ内で index.html ファイルを実行すると、正常に動作します (つまり、円が描画されます)。ただし、ノードを使用してこれを提供しようとすると:

$ node server.js

ブラウザで localhost:8080/ を指定しても円が表示されず、代わりにコンソールに次のエラーが表示されます。

Resource interpreted as Script but transferred with MIME type text/html:        
"http://localhost:8080/raphael-min.js". localhost/:6
Uncaught SyntaxError: Unexpected token < :8080/raphael-min.js:2
Uncaught ReferenceError: Raphael is not defined localhost/:12

私は何を間違っていますか?

4

1 に答える 1

0

これらのモジュールにより、ルーティング ジョブが簡単になります: expressconnect

あなたのコードは問題ありませんが、あなたの http サーバーは情報を送信するだけ'Content-Type': 'text/html' で、ブラウザはあなたjsを as として受け取りますhtml

ここに、静的ノードサーバーの例の要点があります。

https://gist.github.com/jmingov/5894964

そして、ここで静的コンテンツに関する情報を表現します。

http://expressjs.com/api.html#app.use

于 2013-06-30T12:27:45.937 に答える