ensl_gathers/config/express.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-20 09:41:58 +00:00
"use strict";
var fs = require("fs");
var path = require("path");
var morgan = require("morgan");
var express = require("express");
var winston = require("winston");
var config = require("./config.js");
2015-07-29 10:46:30 +00:00
var favicon = require("serve-favicon");
var exphbs = require("express-handlebars");
2015-08-18 09:56:35 +00:00
var cookieParser = require("cookie-parser");
2015-07-20 09:41:58 +00:00
var env = process.env.NODE_ENV || "development";
var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json")));
2015-08-10 23:56:53 +00:00
module.exports = app => {
2016-05-17 14:24:23 +00:00
app.use((req, res, next) => {
res.setHeader('X-GNU', 'Michael J Blanchard');
next();
});
2017-04-18 12:17:01 +00:00
// Enforce HTTPS in production
if (env === 'production') {
app.use((req,res,next) => {
res.setHeader('Strict-Transport-Security', 'max-age=2592000; includeSubdomains'); // Enforce usage of HTTPS; max-age = 30 days
next();
});
}
2015-07-29 10:46:30 +00:00
app.use(express.static(path.join(__dirname, '../public')));
2015-08-18 09:56:35 +00:00
app.use(cookieParser());
2016-01-22 10:12:23 +00:00
app.use(favicon(path.join(__dirname, '../public/favicon.ico')));
2015-07-20 09:41:58 +00:00
// Use winston on production
var log;
if (env !== 'development') {
log = {
stream: {
2015-08-10 23:56:53 +00:00
write: (message, encoding) => {
2015-07-20 09:41:58 +00:00
winston.info(message);
}
}
};
} else {
log = 'dev';
}
if (env !== 'test') app.use(morgan(log));
var hbs = exphbs({
defaultLayout: 'main',
extname: '.hbs'
});
app.engine('.hbs', hbs);
app.set('view engine', '.hbs');
2015-07-29 10:46:30 +00:00
};