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")
5
5
command : "start [folderName]" ,
6
6
desc : "Start HTTP server" ,
7
7
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
+ } ) ;
12
19
} ,
13
- handler : ( argv ) => require ( "./server" ) . serve ( argv . folderName ) ,
20
+ handler : ( argv ) => require ( "./server" ) . serve ( argv . folderName , argv . port ) ,
14
21
} )
15
22
. demandCommand ( ) . argv ;
23
+
Original file line number Diff line number Diff line change @@ -3,29 +3,31 @@ const morgan = require("morgan");
3
3
const path = require ( "path" ) ;
4
4
const { pathToFileURL, fileURLToPath } = require ( "url" ) ;
5
5
6
- function serve ( folderName ) {
6
+ function serve ( folderName , port ) {
7
7
const configPath = pathToFileURL ( path . resolve ( process . cwd ( ) ) ) ;
8
8
9
9
import ( `${ configPath } /config.mjs` ) . then ( ( { default : config } ) => {
10
10
const app = express ( ) ;
11
11
app . use ( morgan ( "tiny" ) ) ;
12
12
13
13
const staticPath = path . join ( fileURLToPath ( configPath ) , folderName ) ;
14
+
14
15
app . use (
15
16
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
17
18
) ;
18
19
19
20
app . get ( "*" , ( req , res ) => {
20
21
res . sendFile ( path . join ( staticPath , "index.html" ) ) ;
21
22
} ) ;
22
23
23
- const port = process . env . PORT || config . port || 3000 ;
24
+ const serverPort = process . env . PORT || port || config . port || 3000 ;
24
25
25
- app . listen ( port , ( ) => {
26
+ app . listen ( serverPort , ( ) => {
26
27
console . log ( `Server running on port ${ port } with base ${ config . base } ` ) ;
27
28
} ) ;
28
29
} ) ;
29
30
}
30
31
31
32
module . exports = { serve } ;
33
+
You can’t perform that action at this time.
0 commit comments