1

when I run

node app.js

I'm getting this error

 info  - socket.io started
   warn  - error raised: Error: listen EACCES

This is all the JavaScript code in the app.

When I run

sudo supervisor app.js

It outputs

DEBUG: Starting child process with 'node app.js'

but when I visit localhost:8080, it says google could not connect to localhost 8080. I set the port in client.js below (line 3).

Can you see what I'm doing wrong?

app.js

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var jade = require('jade');

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
  app.set('port', 80);
  app.set('views', __dirname + '/views');
  app.engine('jade', require('jade').__express);
});

var io = require('socket.io').listen(app.listen(app.get('port')));

app.get('/', function(req, res){
  res.render('layout.jade');
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

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


var socketClient = require('socket.io-client');
var socket = socketClient.connect('https://chat.meatspac.es');

socket.on('message', function(data) {
  io.sockets.emit('newmeat', { meat: data });
});

client.js

(function($) {

  var socket = io.connect('http://localhost:8080');
  var template = $('#template').html();

  socket.on('newmeat', function (data) {

    var copy = $(template).clone();
    var list = $('#meats');
    var images = $('img');

    if (images.length < 9) {

      // search for empty images, fill them with new data
      if ($('[data-empty]').length > 0) {
        $('[data-empty]').first().attr('src', data.meat.chat.value.media).removeAttr('data-empty');
        return;
      }

      // otherwise fill the template in with data
      copy.find('img').attr({
        'src' : data.meat.chat.value.media,
        'height' : '150',
        'width' : '200'
      });

      // if the image is the 4th one appended (counting from zero), create the meatspace logo
      if (images.length == 3) {
        list.append(copy);

        var template2 = $(template).clone();
        template2.find('img').addClass('logo').attr({
          'src' : '/meatspace.svg',
          'height' : '150',
          'width' : '200'
        });
        template2.find('a').addClass('logolink');

        list.append(template2);
      } else {
        list.append(copy);
      }
    }

    // else add new data to elements already present
    else if ($('[data-empty]').length > 0) {
      $('[data-empty]').first().attr('src', data.meat.chat.value.media).removeAttr('data-empty');
      return;
    }
  });

  // remove an unwanted gif
  $('ul').on('click', 'a:not(:eq(4))', function(e) {
    e.preventDefault();

    $(e.currentTarget).find('img').attr({'src' : '', 'data-empty' : ''});
  });

}(jQuery));
Leahcim
  • 40,649
  • 59
  • 195
  • 334
  • 3
    I think you should go to port 80 instead of 8080. (You wrote port 80 in your server code.) – Licson Jan 03 '14 at 15:55
  • Just use port 3000 so you're not stepping on other used ports – tymeJV Jan 03 '14 at 15:56
  • 3
    Trying to use port 80 as non-root gives you an error (If I recall correctly ports below 1024 is restricted to root). – some Jan 03 '14 at 15:58
  • it's not clear to me, should this port from client.js `var socket = io.connect('http://localhost:8080');` be the same as this port from app.js `app.set('port', 80);`? – Leahcim Jan 03 '14 at 16:05
  • @tymeJV which port should I set to 3000, the one in client.js or the one in app.js – Leahcim Jan 03 '14 at 16:07
  • 1
    @Leahcim You must have the same port number in the server and the client if you want them to connect. On the server side it means "listen to that port", and on the client it means "connect to that port". – some Jan 03 '14 at 16:21

1 Answers1

2

On many *nix machines, you can't bind to port 80 unless you elevate with sudo (which you don't want to run your code as).

One solution is to run it on a port above 1024 and then use a reverse proxy like node http-proxy or nginx which listens on 80 and forwards.

That also has other benefits like allowing multiple sites to be addressed on 80 (by different host names) by simply forwarding to different node apps running on different ports.

I have some links in this post: NodeJS Managed Hostings vs VPS

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99