问题描述
我是Node.js的新手,我正尝试在该教程之后创建一个聊天应用程序在socket.io的网站上.我已经在本地计算机上运行了它,但是现在我正尝试将其上传到OpenShift服务器.
I'm new to node.js and I'm trying to create a chat app following the tutorial on socket.io's website. I've got it working on my local machine, but now I'm trying to upload it to an OpenShift Server.
在我的应用中,有以下一行:
In my app, I have the line:
<script src="/socket.io/socket.io.js"></script>
在我的本地计算机上,它可以正常加载,但是在我的服务器上,它返回404错误.我以为我的node_modules
最初可能没有加载,但是在嘘到服务器并检查之后,我发现它们存在.
On my local machine, it loads fine, but on my server, it returns a 404 error. I thought that my node_modules
might not be loading at first, but after shh-ing into the server and checking, I see they are present.
我在package.json
中的依赖关系如下:
My dependencies in my package.json
are as follows:
"dependencies": {
"express": "^3.4.8",
"socket.io": "^1.1.0"
}
关于从socket.io收到404错误,这里有许多不同的问题,我已经抛出了许多问题,他们试图解决此问题,但没有任何进展.包括将script
src更改为:
There are many different questions on here about getting a 404 error from socket.io and I've gone threw many of them them attempting to solve this issue, with no progress. Including changing the script
src to:
http://app-domain.rhccloud.com
http://app-domain.rhccloud.com:8000
http://app-domain.rhccloud.com:8080
ws://app-domain.rhchloud.com:
大多数建议并非特定于OpenShift服务器,因此我想具体询问一下.
Most of the suggestions aren't specific to an OpenShift server so I figured I'd ask specifically about it.
如果需要,这是我的server.js
:
#!/bin/env node
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.listen(port,ip,function() {
console.log('listening');
});
io.on('connection',function(socket) {
console.log('a user connected');
socket.on('disconnect',function() {
console.log('user disconnected');
});
socket.on('chat message',function(msg) {
io.emit('chat message',msg);
});
});
感谢您的帮助.
推荐答案
应为:
server.listen(port,ip,function() {
console.log('listening');
});
不是app.listen
.
这篇关于Socket.io.js在OpenShift上返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!