httpware-leftover
Version:
send 4xx 5xx response, compatible with connect
56 lines (50 loc) • 1.43 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var debug, exports, leftover;
debug = require('debug')('httpware-leftover');
leftover = function(options) {
if (options == null) {
options = {};
}
return function(err, req, res, next) {
debug('leftover - err :', err);
res.statusCode = 404;
if (err) {
res.statusCode = err.status || 500;
if (res.statusCode === 500) {
return leftover.send500(err, req, res, options);
}
}
return leftover.send404(req, res, options);
};
};
leftover.send500 = function(err, req, res, options) {
var html, opt;
debug('leftover ', 'send 500');
opt = options['500'] || {};
html = "<html>\n<body style='padding:20px;'>\n <h1>" + (err.toString()) + "</h1><em>Http Status : 500</em><pre style='margin: 15px'>" + err.stack + "</pre>\n</body>\n</html>";
if (opt.html != null) {
html = opt.html;
if (typeof html === "function") {
return html(err, function(html) {
return res.end(html);
});
}
}
return res.end(html);
};
leftover.send404 = function(req, res, options) {
var html, opt;
debug('leftover ', 'send 404');
opt = options['404'] || {};
html = "404 - Page Not Found";
if (opt.html != null) {
html = opt.html;
if (typeof html === "function") {
return html(function(html) {
return res.end(html);
});
}
}
return res.end(html);
};
module.exports = exports = leftover;