src/server.js
const express = require('express');const bodyParser = require('body-parser');const bunyan = require('bunyan');const helmet = require('helmet');const path = require('path');const routesArray = require('./routes.js');const { metadata, login } = require('./saml2-config'); // define full path to static buildconst STATIC_PATH = process.env.STATIC_PATH || path.join(__dirname, '../build'); // Are we using a mock SAML for demo purposes?// If so, the env var USE_MOCK_SAML should be 1.const USE_MOCK_SAML = process.env.USE_MOCK_SAML === '1'; // define the API root urlconst API_ROOT = process.env.API_ROOT || 'http://localhost:8000'; // define the prefix for the applicationconst PUBLIC_URL = process.env.PUBLIC_URL || '/talentmap/'; /* eslint-disable no-unused-vars */// Define the SAML login redirectlet SAML_LOGIN = `${API_ROOT}/saml2/acs/`;if (USE_MOCK_SAML) { SAML_LOGIN = `${PUBLIC_URL}login.html`;}/* eslint-enable no-unused-vars */ // Define the SAML logout redirectlet SAML_LOGOUT = `${API_ROOT}/saml2/logout/`;if (USE_MOCK_SAML) { SAML_LOGOUT = `${PUBLIC_URL}login.html`;} // Routes from React, with wildcard added to the end if the route is not exactconst ROUTES = routesArray.map(route => `${PUBLIC_URL}${route.path}${route.exact ? '' : '*'}`.replace('//', '/')); // define the OBC root url// example: https://www.obcurl.govconst OBC_URL = process.env.OBC_URL; // path to external about pageconst ABOUT_PAGE = process.env.ABOUT_PAGE || 'https://github.com/18F/State-TalentMAP'; // application portconst port = process.env.PORT || 3000; // set up loggerconst logger = bunyan.createLogger({ name: 'TalentMAP' }); // logging middlewareconst loggingMiddleware = (request, response, next) => { // object to log const log = { method: request.method, headers: request.headers, url: request.url, query: request.query, }; response.on('error', () => { logger.error(log); }); response.on('finish', () => { logger.info(log); }); next();}; const app = express(); // body parserapp.use(bodyParser.urlencoded({ extended: false })); // remove 'X-Powered-By' headerapp.disable('x-powered-by'); // middleware for HTTP headersapp.use(helmet());app.use(helmet.noCache()); // middleware for static assetsapp.use(PUBLIC_URL, express.static(STATIC_PATH)); app.use(bodyParser.urlencoded({ extended: true,})); // middleware for loggingapp.use(loggingMiddleware); // saml2 acsapp.post(PUBLIC_URL, (request, response) => { response.redirect(307, `${API_ROOT}/saml2/acs/`);}); // saml2 loginapp.get(`${PUBLIC_URL}login`, (request, response) => { // create handler // eslint-disable-next-line no-unused-vars const loginHandler = (err, loginUrl, requestId) => { if (err) { response.sendStatus(500); } else { response.redirect(loginUrl); } }; login(loginHandler);}); app.get(`${PUBLIC_URL}logout`, (request, response) => { response.redirect(`${API_ROOT}/saml2/logout/`);}); // saml2 metadataapp.get(`${PUBLIC_URL}metadata`, (request, response) => { response.type('application/xml'); response.send(metadata);}); // OBC redirect - post data detail// endpoint for post-specific data pointsapp.get(`${PUBLIC_URL}obc/post/data/:id`, (request, response) => { // set the id passed in the route and pass it to the redirect const id = request.params.id; response.redirect(`${OBC_URL}/post/postdatadetails/${id}`);}); app.get(`${PUBLIC_URL}logout`, (request, response) => { response.redirect(SAML_LOGOUT);}); // OBC redirect - posts// endpoint for post, ie landing pageapp.get(`${PUBLIC_URL}obc/post/:id`, (request, response) => { // set the id passed in the route and pass it to the redirect const id = request.params.id; response.redirect(`${OBC_URL}/post/detail/${id}`);}); // OBC redirect - countries// endpoint for country, ie landing pageapp.get(`${PUBLIC_URL}obc/country/:id`, (request, response) => { // set the id passed in the route and pass it to the redirect const id = request.params.id; response.redirect(`${OBC_URL}/country/detail/${id}`);}); app.get(`${PUBLIC_URL}about/more`, (request, response) => { response.redirect(`${ABOUT_PAGE}`);}); app.get(ROUTES, (request, response) => { response.sendFile(path.resolve(STATIC_PATH, 'index.html'));}); // this is our wildcard, 404 routeapp.get('*', (request, response) => { response.sendStatus(404).end();}); const server = app.listen(port); // export the the app and server separatelymodule.exports = { app, server };