Nodejs中关于使用jquery的一点小问题,求解
Nodejs中关于使用jquery的一点小问题,求解
各位大神,小弟初学node,遇到一个小问题,度娘问不出答案,只好求教。跪谢。。。 用socket.io实现ping和pong对话时,引用了jquery。 代码如下: 服务器端:
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
fs.readFile('./index.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data, 'utf-8');
});
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.on('ping', function (data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function (data) {
console.log('Received PONG response. PONG!');
});
setInterval(function() {
console.log('Sending PING to client..');
socket.emit('ping', { text: 'PING' });
}, 10000);
});
客户端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<button id="ping">Send PING to server</button>
<script src="/jquery-1.9.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('ping', function (data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function (data) {
console.log('Received PONG response. PONG!');
});
$('#ping').click(function() {
console.log('Sending PING to server..')
socket.emit('ping', { text: 'PING' });
});
</script>
</body>
</html>
我的jquery-1.9.1.min.js文件就放在上面两个文件同一目录下,可是运行的时候ie却报出如下错误:
网页错误详细信息
消息: 语法错误
行: 1
字符: 1
代码: 0
URI: http://localhost:3000/jquery-1.9.1.min.js
代码里的引用确实是 <script src="/jquery-1.9.1.min.js"></script>
啊,jquery-1.9.1.min.js文件也存在啊,到底怎么回事呢,我折腾了一晚上了,崩溃边缘,求各位大神帮帮小弟。先跪谢了。
另求解: URI: localhost:3000/jquery-1.9.1.min.js里的localhost:3000/是指的哪个目录。
在你的描述中,问题主要集中在如何正确引用和加载 jQuery 文件,以及如何处理客户端与服务器之间的 Socket.IO 通信。以下是一些可能的解决方案和建议:
解决方案
-
确保jQuery文件路径正确:
- 在你的 HTML 文件中,
<script src="/jquery-1.9.1.min.js"></script>
这一行假设 jQuery 文件位于服务器的根目录(即http://localhost:3000/jquery-1.9.1.min.js
)。如果 jQuery 文件不在根目录,你需要调整路径以匹配实际位置。
- 在你的 HTML 文件中,
-
确保静态文件服务正确配置:
- 你可以在服务器端使用
express.static
中间件来提供静态文件,这样你可以更方便地管理静态资源的位置。
- 你可以在服务器端使用
示例代码
服务器端代码
var http = require('http');
var fs = require('fs');
var express = require('express');
var app = express();
app.use(express.static(__dirname)); // 使当前目录下的文件可被访问
var server = http.createServer(app).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
var io = require('socket.io')(server);
io.sockets.on('connection', function(socket) {
socket.on('ping', function(data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function(data) {
console.log('Received PONG response. PONG!');
});
setInterval(function() {
console.log('Sending PING to client..');
socket.emit('ping', { text: 'PING' });
}, 10000);
});
客户端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<button id="ping">Send PING to server</button>
<script src="/jquery-1.9.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('ping', function(data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function(data) {
console.log('Received PONG response. PONG!');
});
$('#ping').click(function() {
console.log('Sending PING to server..');
socket.emit('ping', { text: 'PING' });
});
</script>
</body>
</html>
关于 localhost:3000/
localhost:3000/
是指本地主机上的端口 3000。当你在浏览器中访问http://localhost:3000/
时,你会看到由 Node.js 服务器提供的页面。
通过上述调整,你应该能够解决 jQuery 文件加载失败的问题,并且客户端与服务器之间的 Socket.IO 通信应该可以正常工作。
楼主没个请求都返回 HTML, 结果当然出错了
localhost:3000/
没有对应目录, Node 脚本中的目录是一般脚本文件所在的目录
谢谢解答,小弟再研究研究
根据你的描述,错误发生在浏览器尝试加载 jquery-1.9.1.min.js
文件时,可能是因为路径配置不正确或者文件没有正确地通过服务器提供。
首先,你需要确保 jquery-1.9.1.min.js
文件确实存在于你的项目目录中,并且可以通过服务器访问到。在这个例子中,你需要确认 jquery-1.9.1.min.js
文件位于与 index.html
文件相同的目录下。
其次,在你的 Node.js 服务器代码中,你需要确保服务器能够提供静态文件(如 index.html
和 jquery-1.9.1.min.js
)。你可以使用 express
这样的框架来简化这个过程,或者手动处理静态文件。
以下是改进后的代码示例:
改进后的服务器端代码
var http = require('http');
var fs = require('fs');
var path = require('path');
var express = require('express'); // 引入 express 框架
var app = express();
app.use(express.static(path.join(__dirname))); // 设置静态文件目录为当前目录
var server = http.createServer(app).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
var io = require('socket.io')(server);
io.sockets.on('connection', function (socket) {
socket.on('ping', function (data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function (data) {
console.log('Received PONG response. PONG!');
});
setInterval(function() {
console.log('Sending PING to client..');
socket.emit('ping', { text: 'PING' });
}, 10000);
});
客户端代码
确保你的 HTML 文件中的脚本引用是正确的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<button id="ping">Send PING to server</button>
<script src="/jquery-1.9.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('ping', function (data) {
console.log('Received PING. Sending PONG..');
socket.emit('pong', { text: 'PONG' });
});
socket.on('pong', function (data) {
console.log('Received PONG response. PONG!');
});
$('#ping').click(function() {
console.log('Sending PING to server..')
socket.emit('ping', { text: 'PING' });
});
</script>
</body>
</html>
关于 localhost:3000/
的解释
localhost:3000/
是指运行在本地计算机上的服务器地址和端口。具体来说:
localhost
表示本地主机。3000
是服务器监听的端口号。/
表示访问根路径。
当你访问 http://localhost:3000/
时,你将看到 index.html
页面。如果 index.html
中引用了 jquery-1.9.1.min.js
文件,那么浏览器会尝试从 http://localhost:3000/jquery-1.9.1.min.js
加载 jQuery 文件。