Not able to connect Socket.io in express - node.js

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();

Related

Using http.createServer with Express

Can someone see the problem in this code? It must be something simple but I can't see it.
var express = require('express');
var debug = require('debug')('untitled1:server');
var http = require('http');
var app = express();
var router = express.Router();
app.use('/API',router);
app.route('/book').get(function(req, res) {
res.render('./index');
});
var server = http.createServer(app);
server.listen('3000');
server.on('error', onError);
server.on('listening', onListening);
...
You are missing an view engine. Try to add this line, it should solve your problem.
app.set('view engine', 'jade');

Access the public folder when using node and Express?

I have looked through stackoverflow and read the express documentation, I can't figure out why the app won't run when I implement "app.use(express.static());" does anyone know a fix?
var express = require('express')();
var app = require('express')();
var server = require('http').Server(app);
var io = require("socket.io").listen(server);
//If i use this my app will not start
// app.use(express.static());
app.get('/', function(req, res){
res.sendfile('index.html');
});
//Get input from front-end
io.on('connection', function(socket){
// On input do something
socket.on('directional in', function(unique_id, input, input1){
// send info to index
io.emit('directional out', unique_id, input, input1);
});
});
server.listen(3000, function(){
// Server is running
console.log('listening on *:3000');
});
Any help would be great!
You're not initialising express correctly. The correct way would be as follows:
var express = require('express');
var app = express();
With this, you will be able to do
app.use(express.static(__dirname + '/public'));
All together, a fully functional express app would look like this in its most basic form:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(process.env.PORT || 3000);
Let me know what happens.

Socket.IO with Express.JS routing

Creating my small chat wasn't too hard, but now I am trying to create real-time comments system. My current configuration uses latest Node, Express and Socket.IO and I have no idea how to create separate comment page. How to connect Express' routing and SocketIO when routes are defined before I can open socket connection?
var express = require('express');
var app = express();
var swig = require('swig');
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
app.use(express.static(__dirname + '/public'));
swig.setDefaults({ cache: false });
app.get('/', function (req, res) {
res.render('chat');
});
var server = app.listen(80);
var io = require('socket.io').listen(server);
io.on('connection', function (socket) {
socket.emit('open', messages);
socket.on('message', function (data) {
socket.broadcast.emit('message', data);
});
});

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.

Error while compiling WebStorm Node.js project

I just created a simple project in WebStorm using Express module
then I install mongoose and after that I have tried to connect to MongoDB, but it's giving this exception:
Index.js
var mongoose = require('mongoose');
mongoose.connect('localhost', 'Test');
var schema = mongoose.Schema({ name: 'string' },{age:'int'});
var Human = mongoose.model('Human', schema);
exports.saveHuman = function (req,res){
"use strict";
var Ahs = new Human({name:'Dumy'},{age:24});
Ahs.save(function(error , data ){
if(error){
console.log("Not working");
}
else{
res.send(Ahs.name + "Created !");
}
});
};
exports.index = function(req, res){
"use strict";
res.render('index', { title: 'Express' });
};
app.js
var express, routes, user, http, path;
express = require('express');
routes = require('./routes');
user = require('./routes/user');
http = require('http');
path = require('path');
var app = express();
app.configure(function(){
"use strict";
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
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.configure('development', function(){
"use strict";
app.use(express.errorHandler());
});
app.get('/saveHuman',routes.saveHuman);
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
"use strict";
console.log("Express server listening on port " + app.get('port'));
});
Now, when I run the project it shows this in console .

Resources