2

Im working on couple of sites, both have 2 versions, live one and dev one. http://timeprize.com and http://test.timeprize.com They both run on the same port and server, using express vhost.

My vhost app looks very simple, and is basically this:

var evh = require('express-vhost'),
    express = require('express');

/*... some more variable declarations ...*/

if ( site_enabled('test.timeprize.com') ) {
    /**/
    evh.register('test.timeprize.com', function() {
        var app = express();
        app = require("../test_app/app.js").run_app(http);

        return app;
    }() );
};

if ( site_enabled('timeprize.com') ) {
    evh.register('timeprize.com', function() {
        var app = express();
        app = require("../live_app/app.js").run_app(http);

        return app;
    }() );
};

/*... more sites below ...*/

Im running the code above using "forever" process.

And now the problem. Since the test site, and live site are both running on the same server/port using same process, how do i update/restart the test site, without interrupting the live site ?

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42

1 Answers1

1

To address the:

Since the test site, and live site are both running on the same server/port using same process

This shouldn't occur. Your server should throw an error when a second program attempts to listen to a port already occupied.

To answer the real question:

How do I update/restart the test site, without interrupting the live site?

The answer is don't use forever. Yes, it's easy and whatever but for servers with multiple applications running in sync, the must use program is PM2

  1. Install globally:

[sudo] npm i -g pm2

  1. Start your apps, sudo if it listens to a port below 1024:

[sudo] pm2 start app.js

  1. Restart your apps with:

[sudo] pm2 restart [all|name]

OR

[sudo] pm2 gracefulReload [all|name]

Check out documentation

iSkore
  • 7,394
  • 3
  • 34
  • 59
  • "This shouldn't occur. Your server should throw an error when a second program attempts to listen to a port already occupied." Using same server/port is a requirement for me atm. Thats why i used forever with vhost. Vhost doesnt actually create a new httpServer, it creates one global server, that maps incoming events to individual sites. Does PM2 allow that ? – Rainer Plumer Feb 28 '16 at 15:49
  • Hmm. This sounds like a DNS type of thing. What your saying is `forever` will allow you to start two apps listening to the same port? What if you told Port 53 to properly map things to other ports? – iSkore Feb 28 '16 at 15:55
  • express vhost allows many sites using same port to co-exist by creating a single global app, that in turn creates many sub-apps inside itself. Those sub apps use the same server instance that vhost uses. So there is only one app listening to port 80. This app then sends incoming requests to appropriate sites ( like a proxy but much simpler) – Rainer Plumer Feb 28 '16 at 17:37
  • Ahhh gotcha. So you want to restart the apps and have them "compiled" under this one vhost app – iSkore Feb 28 '16 at 17:41