【发布时间】:2013-10-22 02:54:35
【问题描述】:
之前有人问过这个问题,但在之前的回复中没有找到任何解决方案。
Socket.IO 给了我两个问题:
- 服务器端出现此错误 - 错误 - 监听 EACESS 我阅读了 stackoverflow 并通过发出 sudo 命令来启动服务器解决了这个问题。
-
现在客户端似乎没有根据脚本行找到 socket.io.js 文件 -
我了解使用 chrome 开发人员工具控制台找不到文件,文件上有 404 错误。
我读到这个文件是由服务器动态创建的。但我在根文件夹上做了一个“ls-a”。找不到 socket.io/socket.io.js 文件。
有什么想法吗?
这里是我的服务器代码供参考-
var http = require('http'),
path = require("path"),
url = require("url"),
fs = require("fs"),
mime = require("mime"),
io = require("socket.io").listen(server);
var homepath = ".";
var server = http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname;
var filepath = path.join(homepath, uri);
console.log(filepath);
path.exists(filepath, function (exists) {
if (!exists) {
//404 response
res.writeHead(404, {
"Content-Type": "text/plain"
});
res.write("404 File not Found \n");
res.end();
} else {
if (fs.statSync(filepath).isDirectory()) {
filepath += '/index.html';
filepath = path.normalize(filepath);
}
fs.readFile(filepath, "binary", function (err, data) {
if (err) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.write('500 File read error \n');
res.end();
} else {
var contentType = mime.lookup(filepath);
res.writeHead(200, {
'Content-Type': contentType
});
res.write(data, 'binary');
res.end();
}
});
}
});
//sockets part starts here
io.sockets.on('connection', function (socket) {
socket.on('test', function (data) {
console.log('i got something');
console.log(data.print);
});
});
});
server.listen(3000);
server.on('error', function (e) {
console.log(e);
});
console.log('Server listening on Port 3000');
【问题讨论】:
-
你是用 npm 安装的吗?
-
是的,我确实是通过 npm 安装的。
标签: javascript node.js socket.io