X-Git-Url: https://git.auder.net/variants/Baroque/current/gitweb.js?a=blobdiff_plain;f=server%2Fapp.js;h=df15fa20ed4f2902ba1b31fcce2d15f99a1d142b;hb=1006b211894bdc0624e2fd332ac78322bf8ca0ee;hp=13cf1dbf321ce219624b0b08ff23de34b6299968;hpb=77fd729819118d491ff91c1d99960add2ef9cff3;p=vchess.git diff --git a/server/app.js b/server/app.js index 13cf1dbf..df15fa20 100644 --- a/server/app.js +++ b/server/app.js @@ -1,69 +1,69 @@ -var createError = require('http-errors'); -var express = require('express'); -var path = require('path'); -var cookieParser = require('cookie-parser'); -var logger = require('morgan'); -var favicon = require('serve-favicon'); -var params = require('./config/parameters'); +const createError = require('http-errors'); +const express = require('express'); +const path = require('path'); +const cookieParser = require('cookie-parser'); +const logger = require('morgan'); +const favicon = require('serve-favicon'); +const params = require('./config/parameters'); -var app = express(); +let app = express(); app.use(favicon(path.join(__dirname, "static", "favicon.ico"))); if (app.get('env') === 'development') -{ - // Full logging in development mode - app.use(logger('dev')); -} -else -{ - // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/ - app.set('trust proxy', true); - // In prod, only log error responses (https://github.com/expressjs/morgan) - app.use(logger('combined', { - skip: function (req, res) { return res.statusCode < 400 } - })); + // Full logging in development mode + app.use(logger('dev')); +else { + // http://dev.rdybarra.com/2016/06/23/Production-Logging-With-Morgan-In-Express/ + app.set('trust proxy', true); + // In prod, only log error responses (https://github.com/expressjs/morgan) + app.use(logger('combined', { + skip: function (req, res) { return res.statusCode < 400 } + })); } app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); -app.use(express.static(path.join(__dirname, 'static'))); //client "prod" files +// Client "prod" files: +app.use(express.static(path.join(__dirname, 'static'))); +// Update in progress: +app.use(express.static(path.join(__dirname, 'fallback'))); // In development stage the client side has its own server -if (params.cors.enable) -{ - app.use(function(req, res, next) { - res.header("Access-Control-Allow-Origin", params.cors.allowedOrigin); - res.header("Access-Control-Allow-Headers", - "Origin, X-Requested-With, Content-Type, Accept"); - res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"); +if (params.cors.enable) { + app.use(function(req, res, next) { + res.header("Access-Control-Allow-Origin", params.cors.allowedOrigin); + res.header("Access-Control-Allow-Credentials", true); //for cookies + res.header( + "Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept" + ); + res.header( + "Access-Control-Allow-Methods", + "GET, POST, OPTIONS, PUT, DELETE" + ); next(); - }); + }); } // Routing (AJAX-only) const routes = require(path.join(__dirname, "routes", "all")); app.use('/', routes); -// catch 404 and forward to error handler +// Catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); }); -// error handler +// Error handler app.use(function(err, req, res, next) { - // set locals, only providing error in development - res.locals.message = err.message; - res.locals.error = (req.app.get('env') === 'development' ? err : {}); - // render the error page res.status(err.status || 500); - res.send(` - -
#{error.stack}- `); + if (app.get('env') === 'development') console.log(err.stack); + res.send( + "