Cannot GET/ node.js server in heroku - node.js

When I am deploying the app in heroku and running, it gives Cannot GET/ in the browser.
I cannot understand where exactly the problem is !
var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var app = express();
var port = process.env.PORT || 5000;
app.use(express.static(__dirname + "/"));
var server = http.createServer(app);
server.listen(port);
client.js
// When running in heroku
var host = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(host);
// When running on local machine
var connection = new WebSocket('ws://127.0.0.1:5000');
Local Folder Structure:
.git
node_modules // containing ws and express
client.js
index.html // includes client.js
server.js
package.json

Related

Error EADDRINUSE when requiring socket.io server through multiple files

I'm trying to export socket.io server through multiple node script so i can emit notification on the same port.
Here is my main server.js file code:
var express = require('express'),
app = module.exports.app = express();
const options = {};
var server = http.createServer(app);
var io = require('socket.io').listen(server);
exports.io = io;
server.listen(3000, function() {
console.log('Node.js Global app is running...');
});
Below is other node script are runninig when i try to require server.js i get this error:
Error: listen EADDRINUSE 0.0.0.0:3000
server_tn.js
var express = require('express'),
app = module.exports.app = express();
var code_pays = path.basename(__dirname);
console.log('Node.js app is running...' + code_pays);
var main = require('./../main.js');
var importIo = require('./../server');
var io = importIo.io;
main.mainTraitement(code_pays);
You can't have more than one program listenning to a specific port.
Check if you have any program listening on port 3000, or if on your main.js you are also listenning on port 3000.

How to start node express, binaryserver and socket.io on same port?

I have code snippet to explain what i am doing and what i want.
var express = require('express');
var http = require('http');
var app = express();
app.use('/', express.static(__dirname + '/static'));
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);
var binaryServer = new BinaryServer({server:server});
var ioServer = http.createServer(app);
var io = require('socket.io').listen(ioServer);
I can run node express and socket.io on same port.
ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});
Same can be done with node express and binaryServer.
server.listen(8080, function(){
console.log('server running at localhost:8080');
});
But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?
You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});
server.listen(8080, function(){
console.log('http/socket/binary server running at localhost:8080');
});
Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.

socket.io can't connect to server

I'm hosting my application on openshift. I am using a custom domain. And socket.io wasn't able to download the client side script so I just used the cdn instead. But now it's not able to connect to a namespace. These are the errors it is giving me on the console log
This is my client side code on the .html page to download the client side script
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
and the .js index page to connect to the index namespace
var socket = io("http://www.loomius.com/index");
Here is my server side code
var express = require('express');
var app = express();
var http = require('http');
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var https = require('https');
// listening on the port
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || process.env.PORT || 3002);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
http.createServer(app).listen(app.get('port') ,app.get('ip'), function () {
console.log("✔ Express server listening at %s:%d ", app.get('ip'),app.get('port'));
});
First instead of
var socket = io("http://www.loomius.com/index");
use this to solve issue with a need of cdn
var socket = io.connect("/");
Then on server use this instead
http = http.createServer( app ).listen( process.env.PORT, process.env.IP || "0.0.0.0", function() { // or define ip and port manually
var io = require( 'socket.io' )( http );
io.on('connection', function( socket ) {
// add event listeners here
}
});

Nodejs and socket.io in Openshift

I'm doing this simple test on my nodejs app in Openshift. The code below was running fine and I was able to see index.html, until I added the line var io = require('socket.io').listen(http);. Now I'm getting the 503 - Service Temporarily Unavailable. I have socket.io installed in the app-root/repo/node_modules folder, and I've also included it to dependencies in package.json. What could be wrong?
var express = require('express');
var app = express();
var http = require('http').Server(app);
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var io = require('socket.io').listen(http);
app.get('/', function(req, res) {
res.sendfile('index.html');
});
http.listen(server_port, server_ip_address);
Your code works OK on my machine. Double check the dependencies, and above all the logs:
ssh to your gear
less app-root/logs/nodejs.log

Socket.io failed to load resource client side, works on localhost

I am struggling with an Express/Node/PeerJs application that uses socket.io. It works in localhost but not when pushing to heroku or nodejitsu.
Here is what I have in app.js:
var routes = require('./routes');
var express = require('express');
var path = require('path');
var https = require('https');
var fs = require('fs');
var url = require('url');
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io');
app.engine('.html', require('ejs').__express);
app.set('port', process.env.PORT || 5500);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
...
console.log(process.env.port);
app.configure('development', function(){
app.use(express.errorHandler());
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var sockets = io.listen(server);
app.get('/', function(req, res){
res.render('index');
});
//more code
In chatroom.html:
<script type="text/javascript"></script>
<script src="myapp.herokuapp.com:5500/socket.io/socket.io.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
var socket = io.connect('http://myapp.herokuapp.com:5500/');
var peer = new Peer({key: 'mykey', debug: true});
peer.on('open', function(peer) {
peer_id = peer;
console.log('my peer id is ' + peer_id);
socket.emit('peer', {peer_id: peer_id, chatroom: chatroomString});
console.log('peer id and chatroom string: ', {peer_id: peer_id, chatroom: chatroomString});
});
//rest of code
I've tried changing the path to /socket.io/socket.io.js, adding http, changing ports, and many other silly things. Yet unfortunately it works in localhost but not when I push to both nodejitsu and heroku.
There are some relatively similar questions(that I could find) but none with sufficient answers.
Socket.io Failed to load resource
socket.io: Failed to load resource
Would appreciate the help.
SOLUTION!!!
in my app.js file
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var sockets = io;
in my chatroom.html file
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://myapp.herokuapp.com/');
You just take the port numbers out of your html file.
Websockets was not supported earlier in Heroku. Now it is supported. You need to set flag for that.
https://devcenter.heroku.com/articles/heroku-labs-websockets
Even after setting the flag, and restarting the app, the sockets are not working, please clarify the exact error that you are getting.

Resources