Wednesday, July 3, 2013

baseapp: Administration

baseapp provides all the boilerplate to get your JavaScript web application started off right, this Part 8.

  1. Intro to baseapp
  2. Client-Side Unit Tests
  3. Server-Side Unit Tests
  4. WebDriver Integration Tests
  5. Other Grunt Goodies
  6. Authentication
  7. Continuous Integration
  8. Administration
(or binge read them all on the baseapp wiki!)


Bunyan provides very flexible JSON-based logging solution. Here is its config:

Bunyan = require('bunyan')
, log = Bunyan.createLogger({
    name: "YOUR_APP"
    , serializers : Bunyan.stdSerializers 
    , streams: [{
        type: 'rotating-file'
        , path: 'logs/http.log'
        , period: '1d'
        , count: 3

All server logs are are kept in the 'logs' directory, rolled over every day, and 3 previous logs are kept. Bunyan's standard serializers handle serializing the request and response objects.

The logging routine is here:

app.set('logger', log);

// bunyan logging
app.use(function(req, res, next) {
    var end = res.end;
    req._startTime = new Date();
    res.end = function(chunk, encoding){
        res.end = end;
        res.end(chunk, encoding);{req: req, res: res, total_time: new Date() - req._startTime}, 'handled request/response');


This is the second Express 'use' statement - make sure it's near or at the top (only below the 'favicon' use) to log everything! This is taken directly from the 'connect.Logger' middleware - it basically hooks itself into the response 'end' method to record the total time the request took and the request and response objects.

Note we also stash the 'log' object in Express so other middleware can access it.

Starting and Stopping the Server

baseapp uses the awesome forever module to handle the start, restart, and stop of our server. Take a look at our 'start' and 'stop' commands in package.json:

"start": "node_modules/.bin/forever -a -p logs --minUptime 2000 --spinSleepTime 1000 -l forever.log -o logs/out.log -e logs/err.log start app.js",
"stop": "node_modules/.bin/forever stop app.js",

They delegate the handling of starting and stopping our websever to forever - which once started will monitor the process forever (Hmmm) and restart it if it crashes. However our server must be up for at least 2 seconds before forever will try to restart it - remember our server will kill itself off if it cannot connect to 'redis' so in that case we do not want forever always trying to restart it.

The '--spinSleepTime' option just says wait 1 second before trying to start up the server if it died (the forever default). We use the 'append' option (-a) and put all of forever's logs into our 'logs' directory and that's it!.


% npm start

Will keep our webserver up even when/if it crashes subject to sane constraints. Plus we have a clean way to stop it via:

% npm stop

The server gets its HOST and PORT values to listen on from the environment. Here are its defaults:

app.set('port', process.env.PORT || 3000);
app.set('host', process.env.HOST || '');

To change this:

% PORT=9999 HOST=localhost npm start

Now your server is listening on http://localhost:9999 (your shell may vary!). npm config variables is another way to go.

Forever has a couple more tricks up its sleeve - want to know what process forever has spawned off for you - 'forever list' to the rescue:

% node_modules/.bin/forever list
    info:    Forever processes running
    data:        uid  command             script forever pid   logfile          uptime
    data:    [0] OlRc /usr/local/bin/node app.js 54323   54324 logs/forever.log 0:0:0:3.727

You can edit the columns this command displays but you can read the forever documentation for that.

Finally note the index number '[0]' of our process - want to see the 'forever.log' for it but too lazy to load it up yourself? Here ya go:

% node_modules/.bin/forever logs 0
    data:    app.js:54324 - Express server listening on
    data:    app.js:54324 - Express server listening on
    data:    app.js:54324 - Express server listening on
    data:    app.js:54324 - Express server listening on http://localhost:9999
    data:    app.js:54324 - Express server listening on


  1. Stuck with server level programming or networking issues? Getting computer networking assignment help might be helpful.

  2. Thank you so much for sharing this solution, I was facing some issue on my Persuasive Essay Writing Service website server.

  3. its very boring hobby to learn programing my favorite hobby is to watch netflix movies with my all time favorite streaming services StreaMaestro

  4. Thanks for shairng nice post, When you are stressed, your heart beats faster, your blood pressure rises, your breathing gets faster, Psychology Assignment Help and your hands may shake. Your emotions get triggered and you feel more anxious.

  5. Find notable styles to browse, including; cowhide coats, aircraft coats, parkas, and Detroit Become Human Simon Jericho Leather Jacket. Shop different styles including denim coats. Find the selective Men's creator Jackets and Coats assortment by VERSACE. Purchase online the notorious VERSACE items, extravagance and tastefulness models.

  6. It is a nice time surfing from your great adventure blog is a privilege. It is really an insightful piece. Thank you for taking time and effort to put this entire informative article together. Thanks so much for sharing. lasu cut off mark

  7. A server is a computer program or a machine that provides services or resources to other devices or programs, often over a network. Servers are designed to handle client requests and respond with the requested information, data, or functionality. In general, servers are more powerful and have more resources than client devices, and can handle multiple requests simultaneously. Servers can provide a variety of services such as web hosting, email, file sharing, databases, Dissertation Writing Service London and application hosting. They are an essential component of many networks and play a critical role in facilitating communication and data exchange between different devices and systems.

  8. Expert Assistance: Get expert guidance and support from PhD Dissertation Company, the leading provider of cheap dissertation writing services for students worldwide.