|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const http = require('http'); |
| 4 | +const fs = require('fs'); |
| 5 | +const pipe = require('./helpers/pipe'); |
| 6 | +const color = require('./helpers/getColor'); |
| 7 | + |
| 8 | +/** |
| 9 | + * |
| 10 | + * @param {function} callback |
| 11 | + * @param {number} port |
| 12 | + */ |
| 13 | +let createServer = (callback) => (port) => http |
| 14 | + .createServer(callback) |
| 15 | + .on('error', (err) => console.log(color('red'), `Error! ${err}`)) |
| 16 | + .on('listening', () => { |
| 17 | + console.log(`Listening on ${port}`); |
| 18 | + console.log(`Click to open to link http://localhost:${port}`) |
| 19 | + }); |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * @param {http responce} response |
| 24 | + */ |
| 25 | +let getStream = response => filename => fs |
| 26 | + .createReadStream(filename) |
| 27 | + .on('error', () => { |
| 28 | + console.log(color('red'), "Error! Can't read file 'index.html'. Check, that file is exists & correct."); |
| 29 | + response.end(); |
| 30 | + }); |
| 31 | + |
| 32 | +let serverCallback = (filename) => (request, response) => getStream(response)(filename).pipe(response); |
| 33 | + |
| 34 | +let showHelp = () => void console.log(` |
| 35 | +Usage: node server [options] |
| 36 | +
|
| 37 | +Options: |
| 38 | +
|
| 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 |
| 42 | +`); |
| 43 | + |
| 44 | +/** |
| 45 | + * |
| 46 | + * @param {process.argv} agrv |
| 47 | + */ |
| 48 | +let getPort = pipe( |
| 49 | + agrv => agrv.findIndex(x => x === '--port'), |
| 50 | + index => index === -1 ? null : Number(agrv[index + 1]), |
| 51 | + port => { |
| 52 | + if (isNaN(port)){ |
| 53 | + console.error(color('red'), 'Port must be a Number. For more information use --help'); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + return port || 3000; |
| 57 | + } |
| 58 | +); |
| 59 | + |
| 60 | +/** |
| 61 | + * |
| 62 | + * @param {process.argv} agrv |
| 63 | + */ |
| 64 | +let getFile = pipe( |
| 65 | + argv => argv.findIndex(x => x === '--file'), |
| 66 | + index => index === -1 ? null : argv[index + 1], |
| 67 | + filename => { |
| 68 | + if (filename != null && !new RegExp(/\.html/g).test(filename)) { |
| 69 | + console.error(color('red'), 'Needs `html` file. For more information use --help.'); |
| 70 | + process.exit(1); |
| 71 | + } |
| 72 | + return filename || 'index.html' |
| 73 | + } |
| 74 | +); |
| 75 | + |
| 76 | +let getHelp = pipe( |
| 77 | + argv => argv.findIndex(x => x === '--help' || x === '-h'), |
| 78 | + index => { |
| 79 | + if (index === -1) { return; } |
| 80 | + showHelp(); |
| 81 | + process.exit(0); |
| 82 | + } |
| 83 | +); |
| 84 | + |
| 85 | +void function() { |
| 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) |
| 99 | +}() |
0 commit comments