require('dotenv').load();
require('./utils/webpack-code-split-polyfill');

if (process.env.OPBEAT_ID) {
  console.log('loading opbeat');
  require('opbeat').start({
    appId: process.env.OPBEAT_ID,
    organizationId: process.env.OPBEAT_ORG_ID,
    secretToken: process.env.OPBEAT_SECRET
  });
}

var _ = require('lodash'),
    Rx = require('rx'),
    loopback = require('loopback'),
    boot = require('loopback-boot'),
    expressState = require('express-state'),
    path = require('path'),
    setupPassport = require('./component-passport');

Rx.config.longStackSupport = process.env.NODE_DEBUG !== 'production';
var app = loopback();
var isBeta = !!process.env.BETA;

expressState.extend(app);
app.set('state namespace', '__fcc__');
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(loopback.token());
app.disable('x-powered-by');

boot(app, {
  appRootDir: __dirname,
  dev: process.env.NODE_ENV
});

setupPassport(app);

app.start = _.once(function() {
  app.listen(app.get('port'), function() {
    app.emit('started');
    console.log(
      'freeCodeCamp server listening on port %d in %s',
      app.get('port'),
      app.get('env')
    );
    if (isBeta) {
      console.log('freeCodeCamp is in beta mode');
    }
  });
});

module.exports = app;

// start the server if `$ node server.js`
// in production use `$npm start-production`
// or `$node server/production` to start the server
// and wait for DB handshake
if (require.main === module) {
  app.start();
}

@@ -291,19 +291,21 @@
  %7D);%0A%7D%0A%0A
-var
+const
  _ = req
@@ -318,21 +318,23 @@
 lodash')
-,%0A   
+;%0Aconst
  Rx = re
@@ -344,21 +344,23 @@
 re('rx')
-,%0A   
+;%0Aconst
  loopbac
@@ -382,21 +382,23 @@
 opback')
-,%0A   
+;%0Aconst
  boot = 
@@ -421,21 +421,23 @@
 k-boot')
-,%0A   
+;%0Aconst
  express
@@ -468,21 +468,23 @@
 -state')
-,%0A   
+;%0Aconst
  path = 
@@ -498,21 +498,23 @@
 ('path')
-,%0A   
+;%0Aconst
  setupPa
@@ -626,19 +626,21 @@
 ction';%0A
-var
+const
  app = l
@@ -654,11 +654,13 @@
 ();%0A
-var
+const
  isB
@@ -1023,16 +1023,111 @@
 (app);%0A%0A
+const %7B db %7D = app.datasources;%0Adb.on('connected', _.once(() =%3E console.log('db connected')));%0A
 app.star
@@ -1147,24 +1147,39 @@
 nction() %7B%0A 
+ const server =
  app.listen(
@@ -1432,16 +1432,509 @@
 ;%0A    %7D%0A
+    console.log(%60connecting to db at $%7Bdb.settings.url%7D%60);%0A  %7D);%0A%0A  process.on('SIGINT', () =%3E %7B%0A    console.log('Shutting down server');%0A    server.close(() =%3E %7B%0A      console.log('Server is closed');%0A    %7D);%0A    console.log('closing db connection');%0A    db.disconnect()%0A      .then(() =%3E %7B%0A        console.log('DB connection closed');%0A        // exit process%0A        // this may close kept alive sockets%0A        // eslint-disable-next-line no-process-exit%0A        process.exit(0);%0A      %7D);%0A
   %7D);%0A%7D)

Add sigint kill script