From d699ceee4d5c2634aaa8b7d5e5546b9b0eda1239 Mon Sep 17 00:00:00 2001 From: tanmoythander Date: Mon, 8 Oct 2018 01:07:15 +0600 Subject: [PATCH] removed bin/www --- app.js | 206 ++++++++++++++++++++++++++++++++++++++------------- bin/www | 91 ----------------------- package.json | 2 +- 3 files changed, 154 insertions(+), 145 deletions(-) delete mode 100644 bin/www diff --git a/app.js b/app.js index 2e299f0..133437f 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,8 @@ var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var delay = require('delay'); +var debug = require('debug')('express:server'); +var http = require('http'); //initialize mongoose schemas @@ -31,58 +33,7 @@ var dbOptions = { var reconnectTries = 0; var trialDelay = 1; -function delayString(seconds) { - var sec = seconds % 60; - seconds -= sec; - var min = seconds / 60; - var temp = min; - min %= 60; - var hour = (temp - min) / 60; - - var str = ''; - if (hour>0) { - str += hour; - str += ' hour' - if (hour>1) str += 's'; - if (min>0 || sec>0) str += ', '; - } - if (min>0) { - str += min; - str += ' minute' - if (min>1) str += 's'; - if (sec>0) str += ', '; - } - if (sec>0) { - str += sec; - str += ' second' - if (sec>1) str += 's'; - } - return str; -} -function dbConnect() { - console.log('Connecting database ...'); - mongoose.connect( - // Replace CONNECTION_URI with your connection uri - 'CONNECTION_URI', - dbOptions - ).then(function() { - console.log('Database connection successful !!!'); - console.log('Server is fully functional'); - }, function(err) { - console.log('Database connection failed'); - - reconnectTries++; - console.log('Reconnecting after '+delayString(trialDelay)); - console.log('Reconnect trial: '+reconnectTries); - console.log(''); - delay(trialDelay*1000).then(function() { - trialDelay += trialDelay; - if (trialDelay>7200) trialDelay = 7200; - // enable recurtion - dbConnect(); - }); - }); -} +// Connect to database dbConnect(); // CORS Config @@ -161,4 +112,153 @@ app.use(function(err, req, res, next) { }); }); -module.exports = app; +//////////////// Initialize Server ///////////////// + +/** + * Get port from environment and store in Express. + */ + +var port = normalizePort(process.env.PORT || '3484'); +app.set('port', port); + +/** + * Create HTTP server. + */ + +var server = http.createServer(app); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(port); +server.on('error', onError); +server.on('listening', onListening); +console.log('HTTP server running on, localhost:'+port); + +///////////////// Functions ////////////////// + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTP server "error" event. + */ + +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTP server "listening" event. + */ + +function onListening() { + var addr = server.address(); + var bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} + +/** + * Create the delay string + * Requires time in second as parameter + * Returns the time string + */ + +function delayString(seconds) { + var sec = seconds % 60; + seconds -= sec; + var min = seconds / 60; + var temp = min; + min %= 60; + var hour = (temp - min) / 60; + + var str = ''; + if (hour>0) { + str += hour; + str += ' hour' + if (hour>1) str += 's'; + if (min>0 || sec>0) str += ', '; + } + if (min>0) { + str += min; + str += ' minute' + if (min>1) str += 's'; + if (sec>0) str += ', '; + } + if (sec>0) { + str += sec; + str += ' second' + if (sec>1) str += 's'; + } + return str; +} + +/** + * Connect to database + * Recursive reconnect trial upon connection failure + */ + +function dbConnect() { + console.log('Connecting database ...'); + mongoose.connect( + // Replace CONNECTION_URI with your connection uri + 'CONNECTION_URI', + dbOptions + ).then(function() { + console.log('Database connection successful !!!'); + console.log('Server is fully functional'); + }, function(err) { + console.log('Database connection failed'); + + reconnectTries++; + console.log('Reconnecting after '+delayString(trialDelay)); + console.log('Reconnect trial: '+reconnectTries); + console.log(''); + delay(trialDelay*1000).then(function() { + trialDelay += trialDelay; + if (trialDelay>7200) trialDelay = 7200; + // enable recurtion + dbConnect(); + }); + }); +} diff --git a/bin/www b/bin/www deleted file mode 100644 index a91419d..0000000 --- a/bin/www +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env node - -/** - * Module dependencies. - */ - -var app = require('../app'); -var debug = require('debug')('express:server'); -var http = require('http'); - -/** - * Get port from environment and store in Express. - */ - -var port = normalizePort(process.env.PORT || '3484'); -app.set('port', port); - -/** - * Create HTTP server. - */ - -var server = http.createServer(app); - -/** - * Listen on provided port, on all network interfaces. - */ - -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); -console.log('HTTP server running on, localhost:'+port); - -/** - * Normalize a port into a number, string, or false. - */ - -function normalizePort(val) { - var port = parseInt(val, 10); - - if (isNaN(port)) { - // named pipe - return val; - } - - if (port >= 0) { - // port number - return port; - } - - return false; -} - -/** - * Event listener for HTTP server "error" event. - */ - -function onError(error) { - if (error.syscall !== 'listen') { - throw error; - } - - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; - - // handle specific listen errors with friendly messages - switch (error.code) { - case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); - break; - case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); - break; - default: - throw error; - } -} - -/** - * Event listener for HTTP server "listening" event. - */ - -function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - debug('Listening on ' + bind); -} diff --git a/package.json b/package.json index b8e79c3..e9133df 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.0.5", "private": true, "scripts": { - "start": "node ./bin/www" + "start": "node app.js" }, "dependencies": { "assert": "^1.4.1",