'use strict';

var express = require('express');
var bodyParser = require('body-parser');
var Alexa = require('alexa-sdk');
var https = require('https');
var fs = require('fs');
var app = express();
var ngrok = require('ngrok');
var config = require('./config');
var _ = require('lodash');

// Skill handlers
var handlers = require('./app').handlers;

app.use(bodyParser.json());

app.post('/', function (req, res) {
    var context = {
        fail: function () {
            res.sendStatus(500);
        },
        succeed: function (data) {
            res.send(data);
        }
    };

    var alexa = Alexa.handler(req.body, context);
    alexa.registerHandlers(handlers);
    alexa.execute();
});

app.get('/', function (req, res) {
    res.json({
        "text": "hello secure world"
    })
});


https.createServer({
    key: fs.readFileSync(config.get('certs:privateKey')),
    cert: fs.readFileSync(config.get('certs:certificate'))
}, app).listen(config.get('port'), function () {
    console.log('TV Cast app listening on port ' + config.get('port'));

    // If ngrok has been configured set it up
    var ngrokConfig = config.get('ngrok');
    if (ngrokConfig) {
        console.log('Setting up ngrok');
        ngrok.connect(ngrokConfig, function(err, path){
            console.log(arguments)
            if (err){
                console.log('Unable to setup ngrok', err);
                process.exit(1);
            } else {
                console.log('TV Cast app available ' + path);
            }
        });
    }

});

@@ -1270,43 +1270,8 @@
 h)%7B%0A
-            console.log(arguments)%0A
     
@@ -1342,15 +1342,41 @@
 grok
-', 
+ due to: ' + JSON.stringify(
 err)
+)
 ;%0A  
@@ -1499,24 +1499,24 @@
           %7D%0A
-
         %7D);%0A
@@ -1515,16 +1515,174 @@
     %7D);%0A
+%0A        process.on('exit', function()%7B%0A            ngrok.kill();%0A        %7D);%0A        process.on('SIGHUP', function()%7B%0A            ngrok.kill();%0A        %7D);%0A%0A
     %7D%0A%0A%7D

Add exit hooks for ngrok