Comparing version 1.1.0 to 2.0.0
135
index.js
@@ -1,134 +0,1 @@ | ||
var EventEmitter = require('events').EventEmitter, | ||
util = require('util'), | ||
path = require('path'), | ||
logtastic = require('logtastic'), | ||
express = require('express'), | ||
compress = require('compression'), | ||
consolidate = require('consolidate'), | ||
cookieParser = require('cookie-parser'), | ||
errorHandler = require('errorhandler'), | ||
gracefulExit = require('express-graceful-exit'); | ||
var Server = module.exports = function(options) { | ||
// Setup the defualts | ||
options = options || {}; | ||
options.port = options.port || Server.defaultOptions.port; | ||
options.hostname = options.hostname || Server.defaultOptions.hostname; | ||
options.logDir = options.logDir || Server.defaultOptions.logDir; | ||
options.logger = options.logger || Server.defaultOptions.logger; | ||
options.trustProxy = typeof options.trustProxy !== 'undefined' ? options.trustProxy : Server.defaultOptions.trustProxy; | ||
options.compress = typeof options.compress !== 'undefined' ? options.compress : Server.defaultOptions.compress; | ||
options.errorHandler = typeof options.errorHandler !== 'undefined' ? options.errorHandler : Server.defaultOptions.errorHandler; | ||
options.parseCookies = typeof options.parseCookies !== 'undefined' ? options.parseCookies : Server.defaultOptions.parseCookies; | ||
options.viewDir = options.viewDir || Server.defaultOptions.viewDir; | ||
options.viewEngine = options.viewEngine || Server.defaultOptions.viewEngine; | ||
options.viewEngineSuffix = options.viewEngineSuffix || Server.defaultOptions.viewEngineSuffix; | ||
this.options = options; | ||
// Where we will keep the server | ||
this.server = null; | ||
// Create express server | ||
this.app = express(); | ||
this.app.set('port', options.port); | ||
this.app.set('hostname', options.hostname); | ||
this.app.set('trust proxy', options.trustProxy); | ||
this.app.set('x-powered-by', false); | ||
// Setup the logger | ||
this.logger = options.logger; | ||
this.logger.outfile = path.join(options.logDir, 'stdout.log'); | ||
this.logger.errfile = path.join(options.logDir, 'stderr.log'); | ||
this.logger.logUncaught(); | ||
// Log errors | ||
this.app.on('clientError', this.logger.error); | ||
// Setup the views | ||
if (options.viewDir && options.viewEngine) { | ||
this.app.engine('html', consolidate[options.viewEngine]); | ||
this.app.set('view engine', options.viewEngineSuffix); | ||
this.app.set('views', options.viewDir); | ||
} | ||
// Setup middleware | ||
this.app.use(gracefulExit.middleware(this.app)); | ||
this.app.use(this.logger.middleware()); | ||
if (options.compress) { | ||
this.app.use(compress()); | ||
} | ||
if (options.parseCookies) { | ||
this.app.use(cookieParser()); | ||
} | ||
}; | ||
util.inherits(Server, EventEmitter); | ||
Server.defaultOptions = { | ||
port: 3000, | ||
hostname: null, | ||
logDir: 'log', | ||
logger: logtastic, | ||
trustProxy: true, | ||
compress: true, | ||
errorHandler: true, | ||
parseCookies: false, | ||
viewDir: null, | ||
viewEngine: null, | ||
viewEngineSuffix: 'html' | ||
}; | ||
Server.prototype.start = function() { | ||
// If we are in dev mode, add errorHandler | ||
if (this.options.errorHandler && this.app.get('env') !== 'production') { | ||
this.app.use(errorHandler()); | ||
} | ||
this.server = this.app.listen(this.options.port, this.options.hostname, function(err) { | ||
// Currently no error comes through here | ||
// until this happens: https://github.com/strongloop/express/pull/2623 | ||
if (err) { | ||
return this.logger.emergency(err); | ||
} | ||
// On error, clean up and go offline | ||
process.on('uncaughtException', this.stop.bind(this)); | ||
// Listen for the shutdown signal | ||
process.on('message', function(msg) { | ||
if (msg === 'shutdown') { | ||
this.gracefulExit(); | ||
} | ||
}.bind(this)); | ||
// Also listen on sigterm | ||
process.on('SIGTERM', function() { | ||
this.stop(); | ||
}.bind(this)); | ||
if (process.send) { | ||
this.logger.debug('Sending online'); | ||
process.send('online'); | ||
} | ||
this.logger.notice(util.format('Express server listening on port %d in %s mode', this.options.port, this.app.get('env'))); | ||
}.bind(this)); | ||
// Listen for errors on the server | ||
// wont be necessary when the error | ||
// is passed into the listen callback | ||
this.server.on('error', this.logger.emergency); | ||
}; | ||
Server.prototype.stop = function() { | ||
if (process.send) { | ||
this.logger.debug('Sending offline'); | ||
process.send('offline'); | ||
} | ||
this.gracefulExit(); | ||
}; | ||
Server.prototype.gracefulExit = function() { | ||
gracefulExit.gracefulExitHandler(this.app, this.server, { | ||
log: true, | ||
logger: this.logger.info | ||
}); | ||
}; | ||
module.exports = require('./lib/server').AppServer; |
{ | ||
"name": "app-server", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "An express server with logging, zero-downtime, and common-middleware", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"babel": "babel src --out-dir lib", | ||
"test": "npm run babel && happiness" | ||
}, | ||
@@ -22,7 +23,14 @@ "repository": { | ||
}, | ||
"happiness": { | ||
"ignore": [ | ||
"lib" | ||
] | ||
}, | ||
"homepage": "https://github.com/wesleytodd/app-server", | ||
"dependencies": { | ||
"body-parser": "^1.14.2", | ||
"compression": "^1.4.3", | ||
"consolidate": "^0.12.1", | ||
"cookie-parser": "^1.3.5", | ||
"defined": "^1.0.0", | ||
"errorhandler": "^1.3.5", | ||
@@ -32,3 +40,9 @@ "express": "^4.12.3", | ||
"logtastic": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.4.0", | ||
"babel-preset-es2015": "^6.3.13", | ||
"happiness": "git+ssh://git@github.com/JedWatson/happiness.git#standard-fork", | ||
"in-publish": "^2.0.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10167
5
182
9
4
1
+ Addedbody-parser@^1.14.2
+ Addeddefined@^1.0.0
+ Addeddefined@1.0.1(transitive)