Express cannot find socket.io.js
I'm new to Express and Socket.io so I've been having some silly trouble.
I've tried this many ways and I'm not sure where I'm dropping the ball. I
keep getting a 404 Not Found when I look at the inspect element.
heres my code (Jade)
script(src="/socket.io/lib/socket.io.js")
In my express project folder thins are pretty standard. I have
node_modules
public
routes
views
app.js
package.json
I've realized that when I load my CSS and JS files I dont have to write
the src take as:
"/public/javascripts..."
I just write:
"/javascripts..."
I've tried to write :
"/node_modules/socket.io/lib/socket.io.js"
But even that doesnt work.
The weird thing is that that is actually where the file is. But on the
Socket.io page it routes it as:
"/socket.io/socket.io.js"
Update:
I created a folder inside of my public folder called socket.io and I moved
all the library files to it.
It now finds the files but I get a new set of fun errors:
Uncaught reference error: require is not defined
Uncaught reference error: io is not defined
Edit 2:
For the sake of thoroughness, I'll submit my whole code. I think there is
a problem in the socket establishing a connection somehow.
App.js
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, routes = require('./routes')
, user = require('./routes/user')
, path = require('path')
, io = require('socket.io').listen(server);
// all environments
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(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.listen(3000);
app.get('/', routes.index);
app.get('/users', user.list);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Layout.jade (formatted for Zurb Foundation 4)
!!! 5
//if lt IE 7
html.no-js.ie6.oldie(lang='en')
//if IE 7
html.no-js.ie7.oldie(lang='en')
//if IE 8
html.no-js.ie8.oldie(lang='en')
//[if gt IE 8]><!
html.no-js(lang='en')
//<![end if]
head
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge,chrome=1')
title=title
meta(name="description", content=description)
meta(name="author", content=author)
meta(name="viewport", content='width=device-width, initial-scale=1')
link(rel="stylesheet", href="/stylesheets/foundation.min.css")
script(src="/javascripts/vendor/custom.modernizr.js")
// Socket.io CRAP
script(src="/socket.io/socket.io.js")
script.
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
body
.row
.large-12.columns
block content
// Foundation Footer tools
script(src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')
script.
window.jQuery || document.write('<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"><\\/script>')
script(defer, src='/javascripts/foundation.min.js')
No comments:
Post a Comment