无法连接到节点的端口错误

时间:2014-01-03 15:53:42

标签: javascript node.js

当我跑

node app.js

我收到此错误

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

这是应用程序中的所有JavaScript代码。

当我跑步时

sudo supervisor app.js

输出

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

但是当我访问localhost:8080时,它说google无法连接到localhost 8080.我在下面的client.js中设置了端口(第3行)。

你能看出我做错了吗?

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));

1 个答案:

答案 0 :(得分:2)

在许多* nix机器上,除非使用sudo(不想运行代码)提升,否则无法绑定到端口80。

一种解决方案是在1024以上的端口上运行它,然后使用反向代理,如node http-proxy或nginx,它可以侦听80和前进。

这也有其他好处,例如允许通过简单地转发到在不同端口上运行的不同节点应用程序,在80(通过不同的主机名)上寻址多个站点。

我在这篇文章中有一些链接:NodeJS Managed Hostings vs VPS