How to restart / update node express vhost site

248 Views Asked by At

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 ?

1

There are 1 best solutions below

4
On

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