Improve games/challenges display and fix MyGames page reactivity (using () for now...)
[vchess.git] / server / app.js
CommitLineData
c5c47010
BA
1const createError = require('http-errors');
2const express = require('express');
3const path = require('path');
4const cookieParser = require('cookie-parser');
5const logger = require('morgan');
6const favicon = require('serve-favicon');
7const params = require('./config/parameters');
625022fd 8
c5c47010 9let app = express();
625022fd 10
98db2082 11app.use(favicon(path.join(__dirname, "static", "favicon.ico")));
625022fd
BA
12
13if (app.get('env') === 'development')
14{
dac39588
BA
15 // Full logging in development mode
16 app.use(logger('dev'));
625022fd
BA
17}
18else
19{
dac39588
BA
20 // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/
21 app.set('trust proxy', true);
22 // In prod, only log error responses (https://github.com/expressjs/morgan)
23 app.use(logger('combined', {
24 skip: function (req, res) { return res.statusCode < 400 }
25 }));
625022fd
BA
26}
27
28app.use(express.json());
29app.use(express.urlencoded({ extended: false }));
30app.use(cookieParser());
e57c4de4
BA
31// Client "prod" files:
32app.use(express.static(path.join(__dirname, 'static')));
33// Update in progress:
7c42b2ff 34app.use(express.static(path.join(__dirname, 'fallback')));
625022fd
BA
35
36// In development stage the client side has its own server
98db2082 37if (params.cors.enable)
625022fd 38{
dac39588
BA
39 app.use(function(req, res, next) {
40 res.header("Access-Control-Allow-Origin", params.cors.allowedOrigin);
41 res.header("Access-Control-Allow-Credentials", true); //for cookies
e57c4de4
BA
42 res.header(
43 "Access-Control-Allow-Headers",
44 "Origin, X-Requested-With, Content-Type, Accept"
45 );
46 res.header(
47 "Access-Control-Allow-Methods",
48 "GET, POST, OPTIONS, PUT, DELETE"
49 );
77fd7298 50 next();
dac39588 51 });
625022fd
BA
52}
53
54// Routing (AJAX-only)
55const routes = require(path.join(__dirname, "routes", "all"));
56app.use('/', routes);
57
e57c4de4 58// Catch 404 and forward to error handler
625022fd
BA
59app.use(function(req, res, next) {
60 next(createError(404));
61});
62
e57c4de4 63// Error handler
625022fd 64app.use(function(err, req, res, next) {
625022fd 65 res.status(err.status || 500);
0da7d5b8
BA
66 if (app.get('env') === 'development')
67 console.log(err.stack);
866842c3
BA
68 res.send(
69 "<h1>" + err.message + "</h1>" +
0da7d5b8 70 "<h2>" + err.status + "</h2>"
866842c3 71 );
625022fd
BA
72});
73
74module.exports = app;