@@ -23,7 +23,7 @@ let createServer = (callback) => (port) => http
2323 * @param {http responce } response
2424 */
2525let getStream = response => filename => fs
26- . createReadStream ( `./ ${ filename } ` )
26+ . createReadStream ( filename )
2727 . on ( 'error' , ( ) => {
2828 console . log ( color ( 'red' ) , "Error! Can't read file 'index.html'. Check, that file is exists & correct." ) ;
2929 response . end ( ) ;
@@ -32,40 +32,36 @@ let getStream = response => filename => fs
3232let serverCallback = ( filename ) => ( request , response ) => getStream ( response ) ( filename ) . pipe ( response ) ;
3333
3434let showHelp = ( ) => void console . log ( `
35- Welcome to server.js script! This script run the server.
35+ Usage: node server [options]
3636
37- You can use agruments :
37+ Options :
3838
39- --port - to set port for server.
40- Default value: 8080.
41- Example: --port 3000.
42-
43- --file - to set file name for start page.
44- Default value: index.html.
45- Example: --file start.html
39+ --port set port for server. Default value: 8080. Example: --port 3000.
40+ --file set file name for start page. Default value: index.html. Example: --file start.html
41+ --help, -h output usage information
4642` ) ;
4743
4844/**
4945 *
5046 * @param {process.argv } agrv
5147 */
52- let getPort = ( agrv ) => pipe (
48+ let getPort = pipe (
5349 agrv => agrv . findIndex ( x => x === '--port' ) ,
5450 index => index === - 1 ? null : Number ( agrv [ index + 1 ] ) ,
55- index => {
56- if ( isNaN ( index ) ) {
57- console . error ( color ( 'red' ) , 'Index must be a Number. For more information use --help' ) ;
51+ port => {
52+ if ( isNaN ( port ) ) {
53+ console . error ( color ( 'red' ) , 'Port must be a Number. For more information use --help' ) ;
5854 process . exit ( 1 ) ;
5955 }
60- return index || 3000 ;
56+ return port || 3000 ;
6157 }
62- ) ( agrv ) ;
58+ ) ;
6359
6460/**
6561 *
6662 * @param {process.argv } agrv
6763 */
68- let getFile = ( argv ) => pipe (
64+ let getFile = pipe (
6965 argv => argv . findIndex ( x => x === '--file' ) ,
7066 index => index === - 1 ? null : argv [ index + 1 ] ,
7167 filename => {
@@ -75,20 +71,29 @@ let getFile = (argv) => pipe(
7571 }
7672 return filename || 'index.html'
7773 }
78- ) ( argv ) ;
74+ ) ;
7975
80- let getHelp = ( argv ) => pipe (
81- argv => argv . findIndex ( x => x === '--help' ) ,
76+ let getHelp = pipe (
77+ argv => argv . findIndex ( x => x === '--help' || x === '-h' ) ,
8278 index => {
8379 if ( index === - 1 ) { return ; }
8480 showHelp ( ) ;
8581 process . exit ( 0 ) ;
8682 }
87- ) ( argv ) ;
83+ ) ;
8884
8985void function ( ) {
90- let agrv = process . argv . slice ( 2 ) ;
91- getHelp ( agrv ) ;
92- let port = getPort ( agrv ) ;
93- createServer ( serverCallback ( getFile ( agrv ) ) ) ( port ) . listen ( port )
86+ let argv = process . argv . slice ( 2 ) ;
87+
88+ getHelp ( argv ) ;
89+
90+ let port = getPort ( argv ) ;
91+
92+ let server = pipe (
93+ getFile ,
94+ serverCallback ,
95+ createServer ,
96+ ) ( argv )
97+
98+ server ( port ) . listen ( port )
9499} ( )
0 commit comments