Newbie question. My browser is throwing an exception:
新手问题。我的浏览器抛出异常:
"Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:9090/d3/d3.min.js"
. Uncaught SyntaxError: Unexpected token <
“资源被解释为脚本但使用MIME类型text / html传输:”http:// localhost:9090 / d3 / d3.min.js“。未捕获的SyntaxError:意外的令牌<
My code has two libraries, socket.io and d3. Node returns the socket.io library to the browser, but when d3 is requested, node returns the web page instead. I evidently don't understand how to configure things to make this work. (Why does node know how to serve socket.io but not d3?) Thanks!
我的代码有两个库,socket.io和d3。 Node将socket.io库返回给浏览器,但是当请求d3时,节点返回网页。我显然不明白如何配置事情来使这项工作。 (为什么节点知道如何提供socket.io而不是d3?)谢谢!
Here's the code:
这是代码:
test_app.js:
test_app.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, watch = require('watch')
, d3 = require('d3')
app.listen(9090);
function handler (req, res) {
fs.readFile(__dirname + '/test_socket.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading the html file');
}
//res.writeHead(200);
res.writeHead(200,{'Content-Type':'text/html'});
res.end(data);
});
}
test_socket.html:
test_socket.html:
<html>
<head>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="../d3/d3.min.js"></script>
</head>
<script>
var socket = io.connect('http://localhost:9090');
socket.on('hi there now', function (data) { // message received from server
console.log(data);
socket.emit('my other event', { my: 'data' }); // talk back to server?
});
</script>
<body>hello</body>
</html>
1 个解决方案
#1
3
I am betting the actual response contains the content of your test_socket.html. The problem is that there is no exception rule for loading the javascript file, the server always responds to requests by sending test_socket.html. The socket.io javscript file works, however, because socket.io itself intercepts this request and serves the socketio.js file (look in your console, it should say "served static content socket.io" or something along those lines)
我打赌实际的响应包含test_socket.html的内容。问题是加载javascript文件没有例外规则,服务器总是通过发送test_socket.html来响应请求。但是,socket.io javscript文件可以正常工作,因为socket.io本身拦截了这个请求并提供了socketio.js文件(在你的控制台中查看,应该说“服务静态内容socket.io”或者其他内容)
I find that it is a lot easier to let a prebuilt framework deal with these kind of issues. Take a look at http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482. It's the tutorial that I used to get started, which introduces you into node.js, npm, express, jade and socket.io. Should get you started on building your first Web 2.0 app quite nicely!
我发现让预构建的框架处理这类问题要容易得多。请查看http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482。这是我以前开始的教程,它向您介绍了node.js,npm,express,jade和socket.io。应该让你开始构建你的第一个Web 2.0应用程序非常好!
#1
3
I am betting the actual response contains the content of your test_socket.html. The problem is that there is no exception rule for loading the javascript file, the server always responds to requests by sending test_socket.html. The socket.io javscript file works, however, because socket.io itself intercepts this request and serves the socketio.js file (look in your console, it should say "served static content socket.io" or something along those lines)
我打赌实际的响应包含test_socket.html的内容。问题是加载javascript文件没有例外规则,服务器总是通过发送test_socket.html来响应请求。但是,socket.io javscript文件可以正常工作,因为socket.io本身拦截了这个请求并提供了socketio.js文件(在你的控制台中查看,应该说“服务静态内容socket.io”或者其他内容)
I find that it is a lot easier to let a prebuilt framework deal with these kind of issues. Take a look at http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482. It's the tutorial that I used to get started, which introduces you into node.js, npm, express, jade and socket.io. Should get you started on building your first Web 2.0 app quite nicely!
我发现让预构建的框架处理这类问题要容易得多。请查看http://code.tutsplus.com/tutorials/using-nodejs-and-websockets-to-build-a-chat-service--net-34482。这是我以前开始的教程,它向您介绍了node.js,npm,express,jade和socket.io。应该让你开始构建你的第一个Web 2.0应用程序非常好!