colouring-montreal/app/src/server.tsx

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-09-30 14:50:09 -04:00
/**
* Server-side Express application
* - API methods
* - entry-point to shared React App
*
*/
2019-11-07 02:39:26 -05:00
import pgConnect from 'connect-pg-simple';
import express from 'express';
2018-09-13 14:54:16 -04:00
import session from 'express-session';
2019-08-14 09:05:49 -04:00
import apiServer from './api/api';
2019-11-07 02:39:26 -05:00
import db from './db';
2019-09-06 14:13:45 -04:00
import frontendRoute from './frontendRoute';
2019-11-07 02:39:26 -05:00
import tileserver from './tiles/tileserver';
// create server
const server = express();
// disable header
server.disable('x-powered-by');
// serve static files
server.use(express.static(process.env.RAZZLE_PUBLIC_DIR));
// handle user sessions
2018-09-13 14:54:16 -04:00
const pgSession = pgConnect(session);
const sess: any = { // TODO: remove any
name: 'cl.session',
store: new pgSession({
2018-09-30 11:18:57 -04:00
pgPromise: db,
2019-02-24 14:28:11 -05:00
tableName: 'user_sessions'
}),
secret: process.env.APP_COOKIE_SECRET,
saveUninitialized: false,
resave: false,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
};
if (server.get('env') === 'production') {
// trust first proxy
2019-11-07 03:13:30 -05:00
server.set('trust proxy', 1);
// serve secure cookies
2019-11-07 03:13:30 -05:00
sess.cookie.secure = true;
}
server.use(session(sess));
2018-09-10 05:44:32 -04:00
server.use('/tiles', tileserver);
2019-08-14 09:05:49 -04:00
server.use('/api', apiServer);
server.use(frontendRoute);
export default server;