mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 05:02:23 +08:00
52c1989780
- Fixes #962
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const morgan = require('morgan');
|
|
const path = require('path');
|
|
const merge = require('lodash/merge');
|
|
const helmet = require('helmet');
|
|
const compression = require('compression');
|
|
const cookieParser = require('cookie-parser');
|
|
const {
|
|
BASE_URL,
|
|
BASE_PATH,
|
|
MOUNT_PATH,
|
|
STATIC_URL,
|
|
HELMET_CONFIGURATION,
|
|
} = require('./url');
|
|
const routes = require('./routes');
|
|
const debug = require('debug')('talk:app');
|
|
|
|
const app = express();
|
|
|
|
//==============================================================================
|
|
// APPLICATION WIDE MIDDLEWARE
|
|
//==============================================================================
|
|
|
|
// Add the logging middleware only if we aren't testing.
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
app.use(morgan('dev'));
|
|
}
|
|
|
|
// Trust the first proxy in front of us, this will enable us to trust the fact
|
|
// that SSL was terminated correctly.
|
|
app.set('trust proxy', 1);
|
|
|
|
// Enable a suite of security good practices through helmet. We disable
|
|
// frameguard to allow crossdomain injection of the embed.
|
|
app.use(helmet(merge(HELMET_CONFIGURATION, {
|
|
frameguard: false,
|
|
})));
|
|
|
|
// Compress the responses if appropriate.
|
|
app.use(compression());
|
|
|
|
// Parse the cookies on the request.
|
|
app.use(cookieParser());
|
|
|
|
// Parse the body json if it's there.
|
|
app.use(bodyParser.json());
|
|
|
|
//==============================================================================
|
|
// VIEW CONFIGURATION
|
|
//==============================================================================
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'ejs');
|
|
|
|
//==============================================================================
|
|
// ROUTES
|
|
//==============================================================================
|
|
|
|
// Apply the BASE_PATH, BASE_URL, and MOUNT_PATH on the app.locals, which will
|
|
// make them available on the templates and the routers.
|
|
app.locals.BASE_URL = BASE_URL;
|
|
app.locals.BASE_PATH = BASE_PATH;
|
|
app.locals.MOUNT_PATH = MOUNT_PATH;
|
|
app.locals.STATIC_URL = STATIC_URL;
|
|
|
|
debug(`mounting routes on the ${MOUNT_PATH} path`);
|
|
|
|
// Actually apply the routes.
|
|
app.use(MOUNT_PATH, routes);
|
|
|
|
module.exports = app;
|