app/app.js
/*
* Copyright 2016 e-UCM (http://www.e-ucm.es/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* This project has received funding from the European Union’s Horizon
* 2020 research and innovation programme under grant agreement No 644187.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 (link is external)
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
logger = require('morgan');
var app = express();
app.config = require((process.env.NODE_ENV === 'test') ? './config-test' : './config');
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev', {
skip: function (req, res) { return false;}
}));
app.use(bodyParser.json({limit: app.config.maxSizeRequest}));
app.use(bodyParser.urlencoded({extended: false, limit: app.config.maxSizeRequest}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (req, res, next) {
res.sendDefaultSuccessMessage = function () {
res.json({
message: 'Success.'
});
};
next();
});
app.use('/health', require('./health'));
app.use('/', require('./viewRoutes'));
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('view/error', {
message: err.message,
error: err
});
});
module.exports = app;