File tree Expand file tree Collapse file tree 2 files changed +19
-9
lines changed
Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,19 @@ require("yargs")
55 command : "start [folderName]" ,
66 desc : "Start HTTP server" ,
77 builder : ( yargs ) => {
8- yargs . positional ( "folderName" , {
9- describe : "Set folder name for the server" ,
10- default : "dist" ,
11- } ) ;
8+ yargs
9+ . positional ( "folderName" , {
10+ describe : "Set folder name for the server" ,
11+ default : "dist" ,
12+ } )
13+ . option ( "port" , {
14+ alias : "p" ,
15+ describe : "Port to run the server on" ,
16+ type : "number" ,
17+ default : 3000 ,
18+ } ) ;
1219 } ,
13- handler : ( argv ) => require ( "./server" ) . serve ( argv . folderName ) ,
20+ handler : ( argv ) => require ( "./server" ) . serve ( argv . folderName , argv . port ) ,
1421 } )
1522 . demandCommand ( ) . argv ;
23+
Original file line number Diff line number Diff line change @@ -3,29 +3,31 @@ const morgan = require("morgan");
33const path = require ( "path" ) ;
44const { pathToFileURL, fileURLToPath } = require ( "url" ) ;
55
6- function serve ( folderName ) {
6+ function serve ( folderName , port ) {
77 const configPath = pathToFileURL ( path . resolve ( process . cwd ( ) ) ) ;
88
99 import ( `${ configPath } /config.mjs` ) . then ( ( { default : config } ) => {
1010 const app = express ( ) ;
1111 app . use ( morgan ( "tiny" ) ) ;
1212
1313 const staticPath = path . join ( fileURLToPath ( configPath ) , folderName ) ;
14+
1415 app . use (
1516 config . base || "/" ,
16- express . static ( staticPath , { redirect : false } ) , // Disable automatic redirection in static middleware
17+ express . static ( staticPath , { redirect : false } ) // Disable automatic redirection in static middleware
1718 ) ;
1819
1920 app . get ( "*" , ( req , res ) => {
2021 res . sendFile ( path . join ( staticPath , "index.html" ) ) ;
2122 } ) ;
2223
23- const port = process . env . PORT || config . port || 3000 ;
24+ const serverPort = process . env . PORT || port || config . port || 3000 ;
2425
25- app . listen ( port , ( ) => {
26+ app . listen ( serverPort , ( ) => {
2627 console . log ( `Server running on port ${ port } with base ${ config . base } ` ) ;
2728 } ) ;
2829 } ) ;
2930}
3031
3132module . exports = { serve } ;
33+
You can’t perform that action at this time.
0 commit comments