Socket.io Failed to load resource - node.js

i have error
Failed to load resource http://localhost:3250/socket.io/1/?t=1376856906369
when i try do something with socket.io (loading work properly)
it is my first lines server
var express = require('express')
, http = require('http')
, mysql = require('mysql');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8080);
app.set('view engine', 'swig');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/lib'));

You are starting the server on port 8080, but you are trying to get socket.io from port 3250. So you either need to change this line:
//server.listen(8080);
server.listen(3250);
...or you use "http://localhost:8080" or just "http://localhost" in your browser.

Related

Not able to connect Socket.io in express

I tried with a different solution to connect to the socket.io but I am not able to connect to it following is my code snippet.
app.js
var express = require('express');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use('/', index);
app.use('/users', users);
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket) {
console.log('Client connected.');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
index.jade
extends layout
script(src='/socket.io/socket.io.js')
script.
var socket = io();
Package.json after installing socket.io
"socket.io": "^2.0.3"
Please suggest me where I am doing wrong
Try with new key word, may be it will work
var app = new express();

Organizing my routes in express js

I have this code that is being used by my app to do some basic CRUD and all the CRUD code resides in customers.js
I would like to have another file media.js that provides functionality to upload files and play multimedia.
How would i modify my routes to take care of the new file media.js.
This is my code
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
//load customers route
var customers = require('./routes/customers');
var app = express();
var connection = require('express-myconnection');
var mysql = require('mysql');
// all environments
app.set('port', process.env.PORT || 4300);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
/*------------------------------------------
connection peer, register as middleware
type koneksi : single,pool and request
-------------------------------------------*/
app.use(
connection(mysql,{
host: 'localhost',
user: 'root',
password : '',
port : 3306, //port mysql
database:'nodejs'
},'request')
);//route index, hello world
app.get('/', routes.index);//route customer list
app.get('/customers', customers.list);//route add customer, get n post
app.get('/customers/add', customers.add);
app.post('/customers/add', customers.save);//route delete customer
app.get('/customers/delete/:id', customers.delete_customer);//edit customer route , get n post
app.get('/customers/edit/:id', customers.edit);
app.post('/customers/edit/:id',customers.save_edit);
app.use(app.router);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I'm not sure if you have ./routes/customers/index.js or ./routes/customers.js.
//load customers route
var customers = require('./routes/customers');
//load media route
var media = require('./routes/media.js');
...
app.get('/media/add', media.add); // You'd need a function defined and exported in your media.js

socket.io connection via client not working

I have just started using node.js socket.io. I am working on a simple connection between client and server.
Here is my server code:
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var socketio = require('socket.io');
var app = express();
app.set('port', process.env.PORT || 1337);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', routes.index);
var server = app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socketio.listen(server);
and this is the client code(http://smartican.com/nodetest.html):
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="socket.io.js"></script>
<script type="text/javascript">
var socket;
socket = io.connect("http://smartican.com:1337/");
$(function() {
alert(socket.transport.sessionid);
});
</script>
</head>
</html>
there seems to be a problem when connecting to the socket from client as line
socket = io.connect("http://smartican.com:1337/");
breaks the script.. I have tested and the socket is open
Your socket.io.js is the wrong javascript file. It's the server javascript not the client javascript. You want socket.io-client.js. Take a look at this answer.

How to correctly set socket.io ports? Getting socket.io.js 404

I have the following setup, according to the documentation:
Client-side:
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect(); // TIP: .connect with no args does auto-discovery
Server-side:
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path');
app = express(),
server = http.createServer(app).listen(80),
io = require('socket.io').listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Why am I getting
GET http://localhost:5000/socket.io/socket.io.js 404 (Not Found)
I tried switching to server = http.createServer(app).listen(process.env.PORT || 3000), bu then I got warn: error raised: Error: listen EACCES and same 404. What am I missing here about the port usage?
You can't create a server on port 80 without special permissions. And on another sidenote, you are trying to create 2 servers (one on port 80 and another one on port 3000).
Here's (a part of) what i'm using for my projects:
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, sio = require('socket.io').listen(server)
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// configure your socket.io instance here
server.listen(app.get('port'), function() {
// server started
});

NodeJS Express not serving '/socket.io/socket.io.js'

I am trying to build NodeJS/Express/SocketIO application.
Imports:
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)...
Configuration:
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
In my Jade template:
script(type='text/javascript', href='/socket.io/socket.io.js')
But '/socket.io/socket.io.js' is not available when I am trying to reach it on
http://localhost:3000/socket.io/socket.io.js
As it says:
Cannot GET /socket.io/socket.io.js
How do I serve socket.io.js from the installed module?
Thanks
You need to copy the client code from an inner Socket.io directory to your public directory. In Socket 0.9, the path to the distributable files is node_modules/socket.io/node_modules/socket.io-client/dist.
You do not have to copy any files, socket.io will take care of the delivery of the file.
var express = require('express'),
app = new express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server)
In Express 3/4 you can initialize socket.io like so:
var io = require("socket.io")(app.listen(80));
There is an error, use
script(type='text/javascript', src='/socket.io/socket.io.js')
instead of
script(type='text/javascript', href='/socket.io/socket.io.js')

Resources