Socket.io.js returns 404 on OpenShift

霸气de小男生 提交于 2019-12-10 10:43:15

问题


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>

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.

My dependencies in my package.json are as follows:

"dependencies": {
    "express": "^3.4.8",
    "socket.io": "^1.1.0"
  }

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:

Most of the suggestions aren't specific to an OpenShift server so I figured I'd ask specifically about it.

If needed, here's my 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);
    });

});

Thanks for any assistance.


回答1:


Should be:

server.listen(port,ip,function() {
    console.log('listening');
});

not app.listen.



来源:https://stackoverflow.com/questions/26580892/socket-io-js-returns-404-on-openshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!