I've been pulling out my hair trying to figure this problem out. I have a node.js app that works fine in windows. I zipped everything up and put it on my linux box (Ubuntu 12) and installed all of the libraries through npm, and yet I still get a 404 message saying my socket.io.js file cannot be found. I've tried various solutions such as linked to the cdn.socket.io script but that just throws a "require not found" error. My code in my html is as follows:
<script src="/socket.io/socket.io.js"></script>
I've even tried <script src="localhost:4000/socket.io/socket.io.js"></script>
and here is my server side:
var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
...
app.listen(4000);
console.log('server started');
This question is similar, however the answer for it is simply is an updated express semantic which shouldn't apply to my code: socket.io.js not found
If my understanding is correct, the script path should work because when socket.io is running, it should direct that request to the right route. I don't really know what else I should look into for a fix, could it be something with the path in Ubuntu?
Any help would be very much appreciated!
Just tested this:
var io = require('socket.io').listen(8000);
Went to localhost:8000 and the 'welcome to socket.io' message showed up so I know it is running....
paths
node_modules: /home/alex/node_modules/socket.io
my node app: /home/alex/documents/project/app.js
I had the same problem and it seemed that the reason was that something went wrong during the installation of socket.io.
Actually the only workaround I found was to make a new directory, copy all your code there and do a fresh install of the Express and socket.io modules:
npm install express socket.io
Maybe I was lucky, but it worked!
Related
Im want to include socket.io in my project. I installed it using npm install -g socket.io still being in directory my files to the project are located in.
I created script to test if it was successfull
i creater server.js with code
var client=require("socket.io").listen(8080).sockets;
client.on("connection",function(socket){
console.log("Someone has connected")
})
and included in html file
<script type="text/javascript" src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
I tried to run node server.js to see if there will be message in console but there wasnt , . Whad did i do wrong? I a trying to solve this problem but can find solution. Thanks for helping.
You must initialize a connection in html file (on client side).
If you are trying to connect to the host that serves the page you can use:
var socket = io();
or specify address:
var socket = io.connect('http://127.0.0.1:8080');
I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/
the full code for this problem can be seen in the link above.
I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages
npm install socket.io#0.9.10 node-static
and setting up the serverside js as instructed
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
var fileServer = new nstatic.Server('./');
app.listen(8080);
I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.
The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
app.listen(8080);
var file = new nstatic.Server('./');
function handler(request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}
console.log('started')
Note also that the default file to serve to responses at / is index.html.
on WINDOWS ...after install express-seed and node.js for the "blog" tutorial, i get the same cmd prompt after typing node app.js.
another time i got body parser and error handling errors
i tried alot of solutions, even had a local host run with another tutorial, but i would like to run from the blog tutorial due to some slight differences of the set-up.
Of course im a newb, and i know theres tons of answers on the forum, but none are correcting my issue...please help.
and everytime i try to post my report on here it errors me saying i have to indent each line 4 spaces. im just losing in general.
Is there a step im missing? all the tut's say just do 'this' and 'this' and i have a local host running so i can make changes to views. any help?
// Module dependencies.
var express = require('express');
var app = express.createServer();
// Configuration
app.configure( function() {
});
// Routes
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(3000);
what version of node & express are you running?
From the command line you can check with:
node --version
and
express --version
From your code, it looks like an older version of express (version 3 or less), but I'm betting you didn't specify the version on the npm install, which will give you the latest version (4+). There's a lot of breaking changes between those versions, so you can't run old code with the new framework successfully. My bet is that your blog tutorial hasn't been updated to express 4.x yet.
I am trying to debug my routes while using NodeJS and the Express framework. I installed node-inspector and got it fully working and open in a Chrome tab. Nevertheless, it isn't showing anything when I have this code in my index.js route
exports.index = function(req, res){
var $ = require('jquery');
console.log($('.title').val);
res.render('index', { title: 'Express' });
};
I'm wondering what the reason behind this is. I hope someone can help!
Edit: When I place a console.log in the app.js the debugger isn't showing anything either.
Other info:
I am using Windows
I first run node-inspector
And then in a second terminal I run node --debug-brk app.js
Not sure if this is the problem:
But this -> console.log($('.title').val);
should be this -> console.log($('.title').val());
But honestly I don't think jQuery works well with Node on the backend side. I have used it with express on the client side, but not on the server side.
I have seen this jQuery install for Node:
npm install jquery
https://npmjs.org/package/jquery
I haven't used this install with node so I cannot be certain this works.
Ok so I'm basically having the same problem as this. But the answers given there don't work for me. So let me re-explain my problem. First of all, here's my code:
Server-Side Javascript (app.js)
var io = require('socket.io');
...
var sio = io.listen(app);
Client-Side Javascript (client.js)
3: var socket = (s)io.connect('http://localhost:3000');
//the (s) represents testing with and without the s out of desperateness :)
Client-Side Template (Jade)
script(src='/socket.io/socket.io.js')
script(src='/javascripts/client.js')
So from what I've read it seems like socket.io should be handling putting the socket.io.js file there but I'm guessing that I have something configured wrong because it's not doing that. Anyways the errors I get with this are:
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined - client.js line 3
After doing some research it seemed that I could change the jade file to link directly to a stable file. So by changing my code to this:
Client-Side Template (Jade)
script(src='http://cdn.socket.io/stable/socket.io.js')
script(src='/javascripts/client.js')
Then the error that I get from this is:
Uncaught TypeError: Object #<Object> has no method 'connect' - client.js line 3
I've been trying to figure this out for hours now. And it seems that I need to have socket.io-client so I made sure that it is installed. I dunno if this will but I am using Express.js as well and I will give you the layout of my files.
/project
app.js /node_modules package.json /public /routes /views
/project/node_modules
/connect /express /jade /jquery /socket.io /stylus
/project/node_modules/socket.io
/benchmarks index.js Makefile package.json restrict_jsonp.patch
History.md /lib /node_modules Readme.md
/project/node_modules/socket.io/node_modules
/policyfile /redis /socket.io-client
/project/public
/images /javascripts /jquery /stylesheets
/project/public/javascripts
client.js
Anyways, any help would be greatly appreciated! :)
As Felix Loether pointed out, the API for Express changed from 2.* to 3.* After spending way too many hours trying to figure out the best way to re-work my code I decided to re-install express to an earlier version by doing this:
npm install express#2.5.8 -g
There is a lot more support (as of today) for Express 2.* so as I am still learning it's better for me to use the earlier version.