isdp
Version:
ISDP multi-file download service
57 lines (46 loc) • 1.63 kB
JavaScript
//node
var fs = require('fs');
//contrib
var express = require('express');
var bodyParser = require('body-parser');
var winston = require('winston');
var expressWinston = require('express-winston');
//mine
var config = require('./config/config');
var logger = new winston.Logger(config.logger.winston);
var controllers = require('./controllers');
//init express app
var app = express();
app.use(bodyParser.json());
app.use(expressWinston.logger(config.logger.winston));
//jwt auth is optional
if(config.express.jwt) app.use(require('express-jwt')(config.express.jwt));
app.use('/', require('./controllers'));
//error handling
app.use(expressWinston.errorLogger(config.logger.winston));
app.use(function(err, req, res, next) {
if(typeof err == "string") err = {message: err};
logger.error(err);
if(err.stack) {
logger.error(err.stack);
err.stack = "hidden"; //for ui
}
res.status(err.status || 500);
res.json(err);
});
process.on('uncaughtException', function (err) {
//TODO report this to somewhere!
logger.error((new Date).toUTCString() + ' uncaughtException:', err.message)
logger.error(err.stack)
//process.exit(1); //some people think we should do this.. but I am not so sure..
})
exports.app = app;
exports.start = function(cb) {
var port = process.env.PORT || config.express.port || '8080';
var host = process.env.HOST || config.express.host || 'localhost';
app.listen(port, host, function() {
console.log("ISDP request handler listening on %s:%d in %s mode", host, port, app.settings.env);
if(cb) cb();
});
};
;