nextorigin-express-skeleton
Version:
Express app skeleton for nextorigin
171 lines (141 loc) • 5.25 kB
JavaScript
// Generated by IcedCoffeeScript 108.0.11
var Flannel, GracefulExit, Skeleton, bodyParser, compression, express, favicon, http, path, render,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
http = require("http");
path = require("path");
express = require("express");
favicon = require("serve-favicon");
Flannel = require("flannel");
compression = require("compression");
bodyParser = require("body-parser");
render = require("express-rendertype");
GracefulExit = require("express-graceful-exit");
Skeleton = (function() {
Skeleton.prototype.logPrefix = "(Skeleton)";
Skeleton.prototype.port = 3000;
Skeleton.prototype.shutdownTimeout = 2 * 60 + 10;
function Skeleton(options) {
var _ref;
this.options = options;
this.gracefulShutdown = __bind(this.gracefulShutdown, this);
this.errThenGracefulShutdown = __bind(this.errThenGracefulShutdown, this);
this.close = __bind(this.close, this);
this.handleRouteErrors = __bind(this.handleRouteErrors, this);
this.bindRoutes = __bind(this.bindRoutes, this);
this.listening = __bind(this.listening, this);
this.handleListeningError = __bind(this.handleListeningError, this);
this.listen = __bind(this.listen, this);
if (!((_ref = this.Flannel) != null ? _ref.winston : void 0)) {
this.Flannel = Flannel.init({
Console: {
level: "debug"
}
});
this.Flannel.shirt(this);
this.debug("initializing");
}
this.address = this.options.address;
this.port = process.env.PORT || this.options.port || this.port;
this.app || (this.app = express());
this.server = http.createServer(this.app);
this.app.use(this.Flannel.morgan(" info"));
this.loadMiddleware();
this.bindRoutes();
this.handleRouteErrors();
process.on("SIGTERM", this.gracefulShutdown);
process.on("uncaughtException", this.errThenGracefulShutdown);
}
Skeleton.prototype.loadMiddleware = function() {
if (this.options.views) {
this.app.set("views", this.options.views);
}
this.app.set("view engine", "pug");
this.app.use(GracefulExit.middleware(this.app));
this.app.use(compression());
if (this.options["static"]) {
this.app.use(express["static"](this.options["static"].root || this.options["static"], this.options["static"].options || {}));
}
if (this.options.favicon) {
this.app.use(favicon(this.options.favicon));
}
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({
extended: !!this.options.urlencoded_extended
}));
return this.app.use(this.options.render || render.auto("text"));
};
Skeleton.prototype.redirectToHttps = function(req, res, next) {
var dest, forwarded, proto;
if (!(proto = req.headers["x-forwarded-proto"])) {
forwarded = req.headers["forwarded"];
forwarded = /proto=(http[s]?)/.exec(forwarded);
proto = forwarded && forwarded[1];
}
if (proto && proto !== "https") {
dest = "https://" + req.hostname + req.url;
return res.redirect(dest);
}
return next();
};
Skeleton.prototype.health = function(req, res, next) {
return res.send("OK");
};
Skeleton.prototype.listen = function(port) {
if (port == null) {
port = this.port;
}
this.server.on("error", this.handleListeningError);
this.server.on("listening", this.listening);
return this.server.listen(port, this.address);
};
Skeleton.prototype.handleListeningError = function(error) {
var bind;
if (error.syscall !== "listen") {
throw error;
}
bind = typeof this.port === "string" ? "Pipe " + this.port : "Port " + this.port;
switch (error.code) {
case "EACCES":
console.error("" + bind + " requires elevated privileges");
return process.exit(1);
case "EADDRINUSE":
console.error("" + bind + " is already in use");
return process.exit(1);
default:
throw error;
}
};
Skeleton.prototype.listening = function() {
return this.info("listening on " + (this.server.address().address) + ":" + (this.server.address().port));
};
Skeleton.prototype.bindRoutes = function() {
return this.debug("stub for loading routes");
};
Skeleton.prototype.handleRouteErrors = function() {
this.app.use(render.Errors.Error404);
if ((this.app.get("env")) === "development") {
this.app.use(render.FancyErrors.auto("text", null, this.log));
}
return this.app.use(render.Errors.auto("text", null, this.log));
};
Skeleton.prototype.close = function(callback) {
return this.server.close(callback);
};
Skeleton.prototype.errThenGracefulShutdown = function(err) {
this.err(err.stack);
return this.gracefulShutdown();
};
Skeleton.prototype.gracefulShutdown = function() {
return GracefulExit.gracefulExitHandler(this.app, this, {
log: true,
logger: this.Flannel.shirt().info.bind(this),
suicideTimeout: this.shutdownTimeout * 1000
});
};
Skeleton.prototype.delay = function(timeout, fn) {
return setTimeout(fn.bind(this), timeout);
};
return Skeleton;
})();
module.exports = Skeleton;
//# sourceMappingURL=skeleton.js.map