1- const express = require ( 'express' ) ;
2- const bodyParser = require ( 'body-parser' ) ;
3- const fileUpload = require ( 'express-fileupload' ) ;
4- const compression = require ( 'compression' ) ;
5- const config = require ( './lib/config' ) ;
6- const log = require ( './logger' ) . express ;
1+ import bodyParser from "body-parser" ;
2+ import compression from "compression" ;
3+ import express from "express" ;
4+ import fileUpload from "express-fileupload" ;
5+ import { isDebugMode } from "./lib/config.js" ;
6+ import cors from "./lib/express/cors.js" ;
7+ import jwt from "./lib/express/jwt.js" ;
8+ import { express as logger } from "./logger.js" ;
9+ import mainRoutes from "./routes/main.js" ;
710
811/**
912 * App
1013 */
1114const app = express ( ) ;
1215app . use ( fileUpload ( ) ) ;
1316app . use ( bodyParser . json ( ) ) ;
14- app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
17+ app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
1518
1619// Gzip
1720app . use ( compression ( ) ) ;
@@ -20,71 +23,70 @@ app.use(compression());
2023 * General Logging, BEFORE routes
2124 */
2225
23- app . disable ( ' x-powered-by' ) ;
24- app . enable ( ' trust proxy' , [ ' loopback' , ' linklocal' , ' uniquelocal' ] ) ;
25- app . enable ( ' strict routing' ) ;
26+ app . disable ( " x-powered-by" ) ;
27+ app . enable ( " trust proxy" , [ " loopback" , " linklocal" , " uniquelocal" ] ) ;
28+ app . enable ( " strict routing" ) ;
2629
2730// pretty print JSON when not live
28- if ( config . debug ( ) ) {
29- app . set ( ' json spaces' , 2 ) ;
31+ if ( isDebugMode ( ) ) {
32+ app . set ( " json spaces" , 2 ) ;
3033}
3134
3235// CORS for everything
33- app . use ( require ( './lib/express/ cors' ) ) ;
36+ app . use ( cors ) ;
3437
3538// General security/cache related headers + server header
36- app . use ( function ( req , res , next ) {
37- let x_frame_options = ' DENY' ;
39+ app . use ( ( _ , res , next ) => {
40+ let x_frame_options = " DENY" ;
3841
39- if ( typeof process . env . X_FRAME_OPTIONS !== ' undefined' && process . env . X_FRAME_OPTIONS ) {
42+ if ( typeof process . env . X_FRAME_OPTIONS !== " undefined" && process . env . X_FRAME_OPTIONS ) {
4043 x_frame_options = process . env . X_FRAME_OPTIONS ;
4144 }
4245
4346 res . set ( {
44- ' X-XSS-Protection' : ' 1; mode=block' ,
45- ' X-Content-Type-Options' : ' nosniff' ,
46- ' X-Frame-Options' : x_frame_options ,
47- ' Cache-Control' : ' no-cache, no-store, max-age=0, must-revalidate' ,
48- Pragma : ' no-cache' ,
49- Expires : 0
47+ " X-XSS-Protection" : " 1; mode=block" ,
48+ " X-Content-Type-Options" : " nosniff" ,
49+ " X-Frame-Options" : x_frame_options ,
50+ " Cache-Control" : " no-cache, no-store, max-age=0, must-revalidate" ,
51+ Pragma : " no-cache" ,
52+ Expires : 0 ,
5053 } ) ;
5154 next ( ) ;
5255} ) ;
5356
54- app . use ( require ( './lib/express/ jwt' ) ( ) ) ;
55- app . use ( '/' , require ( './routes/main' ) ) ;
57+ app . use ( jwt ( ) ) ;
58+ app . use ( "/" , mainRoutes ) ;
5659
5760// production error handler
5861// no stacktraces leaked to user
59- // eslint-disable-next-line
60- app . use ( function ( err , req , res , next ) {
61-
62- let payload = {
62+ app . use ( ( err , req , res , _ ) => {
63+ const payload = {
6364 error : {
64- code : err . status ,
65- message : err . public ? err . message : ' Internal Error'
66- }
65+ code : err . status ,
66+ message : err . public ? err . message : " Internal Error" ,
67+ } ,
6768 } ;
6869
69- if ( config . debug ( ) || ( req . baseUrl + req . path ) . includes ( 'nginx/certificates' ) ) {
70+ if ( typeof err . message_i18n !== "undefined" ) {
71+ payload . error . message_i18n = err . message_i18n ;
72+ }
73+
74+ if ( isDebugMode ( ) || ( req . baseUrl + req . path ) . includes ( "nginx/certificates" ) ) {
7075 payload . debug = {
71- stack : typeof err . stack !== ' undefined' && err . stack ? err . stack . split ( '\n' ) : null ,
72- previous : err . previous
76+ stack : typeof err . stack !== " undefined" && err . stack ? err . stack . split ( "\n" ) : null ,
77+ previous : err . previous ,
7378 } ;
7479 }
7580
7681 // Not every error is worth logging - but this is good for now until it gets annoying.
77- if ( typeof err . stack !== 'undefined' && err . stack ) {
78- if ( config . debug ( ) ) {
79- log . debug ( err . stack ) ;
80- } else if ( typeof err . public == 'undefined' || ! err . public ) {
81- log . warn ( err . message ) ;
82+ if ( typeof err . stack !== "undefined" && err . stack ) {
83+ logger . debug ( err . stack ) ;
84+ if ( typeof err . public === "undefined" || ! err . public ) {
85+ logger . warn ( err . message ) ;
8286 }
8387 }
8488
85- res
86- . status ( err . status || 500 )
87- . send ( payload ) ;
89+ res . status ( err . status || 500 ) . send ( payload ) ;
8890} ) ;
8991
90- module . exports = app ;
92+ export default app ;
0 commit comments