0

Realized after setting up a simple node.js socket.IO server that it isn't sufficient to handle even the simplest webpages containing script tags.

So I investigating express which is a simple web framework for node.js.

After looking thru the express documentation http://expressjs.com/guide.html I was still confused as to how I simply combine express with socket.IO on a node.js server.

Couple hours of googling later I came across this tutorial https://www.digitalocean.com/community/articles/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , http = require('http');

var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use our express server

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.static(__dirname + '/public'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);


console.log("Express server listening on port 3000");
io.sockets.on('connection', function (socket) {
    console.log('A new user connected!');
    socket.emit('info', { msg: 'The world is round, there is no up or down.' });
});

My question is, would anyone reading this configure their server differently? I don't need anything special, no session handling etc, just the ability to serve html pages containing links to external CSS and javascript files.

Bachalo
  • 6,965
  • 27
  • 95
  • 189

2 Answers2

0
  1. Remove the first app.configure wrapper but leave it's contents. It is useless in general, but especially if you don't pass an argument to it.
  2. Remove methodOverride and bodyParser as you aren't using them
Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • thanks. what about routes? I was getting an error saying it was undefined so I commented out the 2 lines referencing it. But still something wrong as when I load my test index.html page it displays 'Cannot GET' – Bachalo Dec 06 '13 at 16:20
  • I think the remaining issue is where the server is pointing. I have an index.html file in the same folder as my app.'s and in the browser loading 'localhost:3000'. It returns 'Cannot GET' so there is a 404. – Bachalo Dec 06 '13 at 16:29
  • ok figured I'm missing the routes module so did a npm install routes, but now 'express/lib/outer/index.js is throwing an error! I give up...will ditch express and try something simpler like send – Bachalo Dec 06 '13 at 16:54
  • Aye. Slow down. So `'./routes'` isn't an npm module. It's a local file that the author of this snippet had beside their `app.js` file. They just screwed up in their blog and didn't post it. – Peter Lyons Dec 06 '13 at 16:59
0

Thanks for all the replies. Finally have something that works and am posting so someone else may benefit. My first attempt(above) was obviously NOT the simplest solution:)

//npm install express
//npm install socket.io
var express = require('express');
var server = express.createServer();

server
    .use( server.router )
    .use( express.static(__dirname+'/public') )
    .get('/api', function(req, res) {
        res.write('API');
    });

server=server.listen(3000);

var io = require('socket.io');
var socket = io.listen(server);

socket.on('connection', function (client){
  // new client is here!


});
Bachalo
  • 6,965
  • 27
  • 95
  • 189