Advance on client side
[vchess.git] / server / app.js
CommitLineData
625022fd
BA
1var createError = require('http-errors');
2var express = require('express');
3var path = require('path');
4var cookieParser = require('cookie-parser');
5var logger = require('morgan');
6var favicon = require('serve-favicon');
7
8var app = express();
9
98db2082 10app.use(favicon(path.join(__dirname, "static", "favicon.ico")));
625022fd
BA
11
12if (app.get('env') === 'development')
13{
14 // Full logging in development mode
15 app.use(logger('dev'));
16}
17else
18{
19 // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/
20 app.set('trust proxy', true);
21 // In prod, only log error responses (https://github.com/expressjs/morgan)
22 app.use(logger('combined', {
23 skip: function (req, res) { return res.statusCode < 400 }
24 }));
25}
26
27app.use(express.json());
28app.use(express.urlencoded({ extended: false }));
29app.use(cookieParser());
98db2082 30app.use(express.static(path.join(__dirname, 'static'))); //client "prod" files
625022fd
BA
31
32// In development stage the client side has its own server
98db2082 33if (params.cors.enable)
625022fd
BA
34{
35 app.use(function(req, res, next) {
98db2082 36 res.header("Access-Control-Allow-Origin", params.cors.allowedOrigin);
625022fd
BA
37 res.header("Access-Control-Allow-Headers",
38 "Origin, X-Requested-With, Content-Type, Accept");
39 next();
40 });
41}
42
43// Routing (AJAX-only)
44const routes = require(path.join(__dirname, "routes", "all"));
45app.use('/', routes);
46
47// catch 404 and forward to error handler
48app.use(function(req, res, next) {
49 next(createError(404));
50});
51
52// error handler
53app.use(function(err, req, res, next) {
54 // set locals, only providing error in development
55 res.locals.message = err.message;
56 res.locals.error = (req.app.get('env') === 'development' ? err : {});
57 // render the error page
58 res.status(err.status || 500);
59 res.send(`
60 <!doctype html>
61 <h1>= message</h1>
62 <h2>= error.status</h2>
63 <pre>#{error.stack}</pre>
64 `);
65});
66
67module.exports = app;