1
- const express = require ( 'express' ) ; // Duh
2
-
1
+ const express = require ( 'express' ) ; // Import Express.js
3
2
const app = express ( ) ;
4
- app . use ( express . static ( 'public' ) ) ;
3
+ app . use ( express . static ( 'public' ) ) ; // Serve static files from the 'public' directory
5
4
5
+ // Root route
6
6
app . get ( '/' , function ( req , res ) {
7
- if ( req . header ( 'X-Replit-User-Id' ) ) { // Check to see if the user is logged in...
8
- res . redirect ( `/home/?user=${ req . header ( 'X-Replit-User-Name' ) } ` ) ; // They are logged in, redirect them to the home page.
7
+ if ( req . header ( 'X-Replit-User-Id' ) ) { // Check if the user is logged in
8
+ res . redirect ( `/home/?user=${ req . header ( 'X-Replit-User-Name' ) } ` ) ; // Redirect to home page with user name as query parameter
9
9
} else {
10
- res . sendFile ( __dirname + '/public/login.html' ) ; // Send a login page if they are not.
10
+ res . sendFile ( __dirname + '/public/login.html' ) ; // Send login page if user is not logged in
11
11
}
12
12
} ) ;
13
13
14
+ // Home route
14
15
app . get ( '/home' , function ( req , res ) {
15
- // You could do anything you want here...
16
- // You might want to secure it a little, so people have to be logged in to view the page.
17
- res . send ( `<h1>Hello, ${ req . query . user } </h1>` ) ; // You could modify this to render a template, or store the user in a database...
16
+ if ( ! req . query . user ) { // Ensure user parameter is present
17
+ res . redirect ( '/' ) ; // Redirect to login if user parameter is missing
18
+ } else {
19
+ res . send ( `<h1>Hello, ${ req . query . user } </h1>` ) ; // Send a personalized greeting to the user
20
+ }
18
21
} ) ;
19
22
20
- app . listen ( 8080 , function ( ) { // Start the server
23
+ // Start the server on port 8080
24
+ app . listen ( 8080 , function ( ) {
21
25
console . log ( 'Server up!' ) ;
22
26
} ) ;
0 commit comments