11// VATSIM PMP - Simple Express Server
22const express = require ( 'express' )
33const path = require ( 'path' )
4- const fs = require ( 'fs' )
54
65const app = express ( )
76
87// Statische Dateien aus public/ servieren
98app . use ( express . static ( path . join ( __dirname , 'public' ) ) )
109
11- // Helper to inject header/footer into a page
12- function renderWithLayout ( pagePath , res ) {
13- try {
14- // Use path.resolve to ensure absolute paths
15- const headerPath = path . resolve ( __dirname , 'public' , 'header.html' ) ;
16- const footerPath = path . resolve ( __dirname , 'public' , 'footer.html' ) ;
17- const contentPath = path . resolve ( pagePath ) ;
18-
19- // Check if files exist
20- if ( ! fs . existsSync ( headerPath ) ) {
21- console . error ( `Header file not found: ${ headerPath } ` ) ;
22- return res . status ( 500 ) . send ( 'Server Error: Header template missing' ) ;
23- }
24-
25- if ( ! fs . existsSync ( footerPath ) ) {
26- console . error ( `Footer file not found: ${ footerPath } ` ) ;
27- return res . status ( 500 ) . send ( 'Server Error: Footer template missing' ) ;
28- }
29-
30- if ( ! fs . existsSync ( contentPath ) ) {
31- console . error ( `Content file not found: ${ contentPath } ` ) ;
32- return res . status ( 404 ) . sendFile ( path . join ( __dirname , 'public' , '404.html' ) ) ;
33- }
34-
35- // Read files with error handling
36- const header = fs . readFileSync ( headerPath , 'utf8' ) ;
37- const footer = fs . readFileSync ( footerPath , 'utf8' ) ;
38- let page = fs . readFileSync ( contentPath , 'utf8' ) ;
39-
40- // Replace placeholders
41- page = page . replace ( '<!--#HEADER-->' , header ) . replace ( '<!--#FOOTER-->' , footer ) ;
42-
43- // Send the response
44- res . send ( page ) ;
45-
46- } catch ( error ) {
47- console . error ( 'Error in renderWithLayout:' , error ) ;
48- res . status ( 500 ) . send ( 'Server Error: Failed to render page' ) ;
49- }
50- }
51-
52- // Serve header and footer as partials
53- app . get ( '/header.html' , ( req , res ) => {
54- res . sendFile ( path . join ( __dirname , 'public' , 'header.html' ) )
55- } )
56- app . get ( '/footer.html' , ( req , res ) => {
57- res . sendFile ( path . join ( __dirname , 'public' , 'footer.html' ) )
58- } )
59-
60- // Image routes for back1-9
61- app . get ( '/back1' , ( req , res ) => {
62- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '1.png' ) )
63- } )
64-
65- app . get ( '/back2' , ( req , res ) => {
66- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '2.png' ) )
67- } )
68-
69- app . get ( '/back3' , ( req , res ) => {
70- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '3.png' ) )
71- } )
72-
73- app . get ( '/back4' , ( req , res ) => {
74- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '4.png' ) )
75- } )
76-
77- app . get ( '/back5' , ( req , res ) => {
78- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '5.png' ) )
79- } )
80-
81- app . get ( '/back6' , ( req , res ) => {
82- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '6.png' ) )
83- } )
84-
85- app . get ( '/back7' , ( req , res ) => {
86- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '7.png' ) )
87- } )
88-
89- app . get ( '/back8' , ( req , res ) => {
90- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '8.png' ) )
91- } )
92-
93- app . get ( '/back9' , ( req , res ) => {
94- res . sendFile ( path . join ( __dirname , 'public' , 'assets' , 'img' , '9.png' ) )
95- } )
96-
9710// Route für die Hauptseite
9811app . get ( '/' , ( req , res ) => {
99- renderWithLayout ( path . join ( __dirname , 'public' , 'index.html' ) , res )
12+ res . sendFile ( path . join ( __dirname , 'public' , 'index.html' ) )
10013} )
10114
10215// Route für Teilnahme
10316app . get ( '/teilnahme' , ( req , res ) => {
104- renderWithLayout ( path . join ( __dirname , 'public' , 'teilnahme.html' ) , res )
17+ res . sendFile ( path . join ( __dirname , 'public' , 'teilnahme.html' ) )
10518} )
10619
10720// Route für Events
10821app . get ( '/events' , ( req , res ) => {
109- renderWithLayout ( path . join ( __dirname , 'public' , 'events.html' ) , res )
22+ res . sendFile ( path . join ( __dirname , 'public' , 'events.html' ) )
11023} )
11124
11225// Route für Kontakt
11326app . get ( '/kontakt' , ( req , res ) => {
114- renderWithLayout ( path . join ( __dirname , 'public' , 'kontakt.html' ) , res )
27+ res . sendFile ( path . join ( __dirname , 'public' , 'kontakt.html' ) )
11528} )
116-
117- // Route für Anleitung
11829app . get ( '/howto' , ( req , res ) => {
119- renderWithLayout ( path . join ( __dirname , 'public' , 'howto.html' ) , res )
30+ res . sendFile ( path . join ( __dirname , 'public' , 'howto.html' ) )
12031} )
12132
12233// Route für Status
@@ -132,40 +43,19 @@ app.get('/status', (req, res) => {
13243app . get ( '/teilnahme.html' , ( req , res ) => res . redirect ( '/teilnahme' ) )
13344app . get ( '/events.html' , ( req , res ) => res . redirect ( '/events' ) )
13445app . get ( '/kontakt.html' , ( req , res ) => res . redirect ( '/kontakt' ) )
135- app . get ( '/howto.html' , ( req , res ) => res . redirect ( '/howto' ) )
13646
137- // Error handling for static files
138- app . use ( ( err , req , res , next ) => {
139- console . error ( 'Express error:' , err ) ;
140- res . status ( 500 ) . send ( 'Server Error' ) ;
141- } ) ;
142-
143- // 404 Handler - make sure this is the last middleware
47+ // 404 Handler
14448app . use ( ( req , res ) => {
145- console . log ( `404 Not Found: ${ req . originalUrl } ` ) ;
146- res . status ( 404 ) . sendFile ( path . join ( __dirname , 'public' , '404.html' ) ) ;
49+ res . status ( 404 ) . sendFile ( path . join ( __dirname , 'public' , '404.html' ) )
14750} )
14851
149- // Better error handling for uncaught exceptions
150- process . on ( 'uncaughtException' , ( err ) => {
151- console . error ( 'Uncaught exception:' , err ) ;
152- // Don't exit the process, just log the error
153- } ) ;
154-
155- // Better error handling for unhandled promise rejections
156- process . on ( 'unhandledRejection' , ( reason , promise ) => {
157- console . error ( 'Unhandled Rejection at:' , promise , 'reason:' , reason ) ;
158- // Don't exit the process, just log the error
159- } ) ;
52+ // Export für Passenger
53+ module . exports = app
16054
161- // Export for Passenger
162- module . exports = app ;
163-
164- // Start Server when directly executed
55+ // Start Server wenn direkt ausgeführt
16556if ( require . main === module ) {
166- const port = process . env . PORT || 80 ;
57+ const port = process . env . PORT || 80
16758 app . listen ( port , ( ) => {
168- console . log ( `VATSIM PMP running on port ${ port } ` ) ;
169- } ) ;
170- }
171-
59+ console . log ( `VATSIM PMP running on port ${ port } ` )
60+ } )
61+ }
0 commit comments