我正在尝试使用 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
我究竟做错了什么?