Socket.io and express setup - node.js

I' m trying socket.io Cut and pasting the exact code they provide I have an error ! Any ideas I'm running on windows8 and node v0.10.26 ?
var io = require('socket.io')(server);
^
TypeError: object is not a function
at Object.<anonymous> (c:\Users\david wilson\TravelShopOffers\app_socketio:3:30)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Here's the code from their site :
In conjunction with Express
Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);

Will not be the best answer, as I was just discovering and experimenting on express/socket.io a few days ago.
Anyway I came up with the following ( interested in feedback ) :
var app = require('express')();
var server = app.listen(3000);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket)
{
socket.on('myEvent',function(){ /* … */ });
}
);
Hope this will help

Related

Simple NodeJS socketIO server throws error

Trying to debug this simple NodeJS socketIO server.
I keep getting the following error message on launch. I can't see anything wrong with the code.
Can anyone help?
TypeError: listener must be a function
at TypeError ()
at Namespace.EventEmitter.addListener (events.js:130:11)
at Server.(anonymous function) [as on] (/Users/foo/bin/node_modules/socket.io/lib/index.js:364:16)
at Object. (/Users/foo/bin/foo.js:11:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)*
var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);
io.on("connection", handleClient);
var handleClient = function (socket) {
// we've got a client connection
socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};
app.listen(8080);
You're missing some parens:
var app = require('express')();
you have forgotten to take an instance of express. I hope my below example will help you.
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
io.on("connection", handleClient);
var handleClient = function (socket) {
// we've got a client connection
socket.emit("tweet", {user: "nodesource", text: "Hello, world!"});
};
app.listen(8080);

Socket.IO Server Runtime Error/Exception

I have this code in a file named server.js:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(80);
When i run node server.js in the terminal (cmd), it throws this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (C:\inetpub\wwwroot\socketio\server.js:9:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Do you have an idea of what could be the problem? Thanks!
Note: I already have socket.io and express installed.
It seems you are not allowed to open a serversocket on port 80.
See this line in the error message
C:\inetpub\wwwroot\socketio\server.js:9:8
Socket addresses smaller than 1024 are reserved for system use. Try this:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(3000);
Looks like the port 80, is invalid or unacceptable. I tried to change it to another port (8000) and it worked.
server.listen(8000);
The exception is gone. I hope I can find a way to distinguish between valid/invalid ports at run-time.

Node.js error no server startup

As a part of angular.js course, i downloaded and installed node.js, inserted server.js file in main folder with following content :
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and then tried to run server by cli, but im getting error in cli:
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'static'
at Object.<anonymous> (C:\Program Files (x86)\nodejs\server.js:4:19)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
try:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('angularjs'));
app.listen(5000);
Should work for you!
EDIT:
With Connect 3.0 .static() is moved to a separate package called serve-static.
So you'll have to install that before running this code.
Edit: THis is for an older version of Node and therefore doesn't answer the question. See the comments below.
To use connect, you need to setup the environment, and then create the server
var connect = require('connect');
car app = connect();
app.static("../angularjs");
connect.createServer(app).listen(5000);
You may also be able to do it the brief way that you have by:
connect.createServer(
connect().static("../angularjs")
).listen(5000);

Twitter API request error while using node.js

I am trying to make a request to the Twitter API and respond with the JSON.
This is my code.
var http = require('http');
var request = require('request');
var url = require('url');
app.get('/twwets/:username', function(req, response) {
var username = req.params.username;
options = {
protocol: "http:",
host: 'api.twitter.com',
pathname: '/1/statuses/user_timeline.json',
query: { screen_name: username, count: 10}
}
var twitterUrl = url.format(options);
request(twitterUrl).pipe(response);
}).listen(8080);
And this is what I get when I run node app.js in my terminal:
module.js:340
throw err;
^
Error: Cannot find module 'require'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/geosh/node.js_practice/express_practice/app.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I need to know what do I do wrong.
[My code may seem useless, but it's just for educational purposes. Please excuse any mistake.]
EDIT: I still get the same error after I saved and run the code in my shell.
Typo;
var request = require('require');
should most likely be
var request = require('request');
EDIT: You're also missing express, I suspect you'll want to add these lines;
var express = require('express');
var app = express();
...and npm install request express to get the modules installed.
Refer the following links may use
http://expressjs.com/
For Twitter Authentication
http://passportjs.org/

Can't run this nodejs code?

Im trying to run this simple code but I m getting error all the time...I really dont know what's the problem. It seems to me very Ok...? Thanks!
var http = require(http);
console.log('Starting');
var host = '127.0.0.1';
var port = 1337;
var server = http.createServer(function(request, response){
console.log('Receive request: '+ request.url);
response.writeHead(200, {"Content-type" : "text/plain"});
response.end("Hello world");
});
server.listen(port, host, function(){
console.log('Listening: ' + host + ':' + port);
});
Console error is this:
assert.js:98
throw new assert.AssertionError({
^
AssertionError: path must be a string: path must be a string
at Module.require (module.js:362:3)
at require (module.js:380:17)
at Object.<anonymous> (/Users/yoniPacheko/PhpstormProjects/angularJS/nodeJS/server.js:9:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
In your first line there are quotes missing around http:
var http = require('http');
In my case, I had
var http = require('http');
var path= require('path');
As soon as I deleted the the http line, it worked for me.So, i did leave only
var path= require('path');
I was working on express,mongodb,socket.io.

Resources