This is my jade file:
doctype html
html
head
meta(charset="UTF-8")
title
script(src="/socket.io/socket.io.js")
script.
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
body
hello world
and this is my app.js
var app = require('express')()
var server = require('http').Server(app)
var io = require('socket.io')(server);
app.listen(3000)
app.get('/', function (req, res) {
res.render('index.jade')
})
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Socket.io does not work because if I try to get
http://localhost:3000/socket.io/socket.io.js
node server responds:
Cannot GET /socket.io/socket.io.js
I have found this question/answer but does not help. Maybe due to my module version:
{
"name": "venti",
"version": "0.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"socket.io": "^1.0.6",
"express": "^4.6.1",
"jade": "^1.4.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
I copied your code. I also got the same error.
I just changed this line
var server = require('http').Server(app)
to
var server = app.listen(3000);
and it worked. Make sure to remove app.listen(3000) what you already have.