简体   繁体   中英

How do i get node.js express.js on port 80

Hello I am trying to run my node.js webserver on port 80 on Ubuntu 12.04.2. This machine is a clean install and the only thing I have installed is, openssh-server nodejs and screen. When I run the node webservice on port 80, I can navigate to the browser and type in localhost and i will view my site. However when i try to access the site from a different machine i get timed out. I can however SSH and PING the machine. How can i setup ubuntu so that my node.js application is serving my website. The site works just fine when i host it on my laptop(windows 7) and a different laptop access the site by my ip address.

I do not want to run Apache or nginx. Is there anyway to do this?

    //---ExpressJS
    console.log('Initializing Express...');
    var express = require('express');
    var app = express();

    //---Middleware: Allows cross-domain requests (CORS)
    var allowCrossDomain = function(req, res, next){
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
    }

    ///---MemoryStore
    //var MemoryStore = express.session.MemoryStore;

    //---App config
    app.configure(function() {
      var pub_dir = __dirname + '/public';
      app.set('views', __dirname + '/views');
      app.set('view engine', 'jade');
      app.set("trust proxy", true);
      app.use(express.favicon(__dirname+'/favicon.ico'));
      app.use(express.compress());
      app.use(express.bodyParser());
      app.use(express.cookieParser());
      app.use(express.session({secret: 'cogswell'}));
      app.use(express.methodOverride());
      app.use(allowCrossDomain);
      app.use(app.router);
      app.use(express.static(__dirname));
    });

    //---Start listening
    var port = 80;
    app.listen(port);
    console.log('Webservice started on port: '+port);

UPDATE: the network has a massive firewall

Are you running as root? Or at least have permission to use port 80? On Linux systems you need special user privileges to use port 1024 or below. Try running as root using sudo node ...

You're trying to access from another computer? If so then bind to address 0.0.0.0 instead of locahost .

Binding an application to 0.0.0.0 means that it should be accessible from outside the computer (ie bound on all interfaces). Whereas binding to localhost restricts the service to being accessible locally.

To confirm that your application works internally try:

wget -O - http://127.0.0.1/

... on the same computer that is running the web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM