installation:
$ npm install --save express
$ node ./static-server.js [port]
(default port is 3000)
$ chmod +x ./index.js
put it on your $PATH, then:
$ static-server.js [port]
(default port is 3000)
| #! /usr/bin/env node | |
| const express = require('express') | |
| const server = express() | |
| // serve whatever directory you run the command from | |
| const dir = process.cwd() | |
| server.use(express.static(dir)) | |
| // the default port is 3000 | |
| let port = 3000 | |
| // if the last argument passed into the command is a number, | |
| // use that as the port instead | |
| // sometimes parsing fails, but we don't want | |
| // that to halt the program. | |
| try { | |
| const lastArgument = process.argv[process.argv.length - 1] | |
| const number = JSON.parse(lastArgument) | |
| if (typeof number === 'number') port = number | |
| } catch (e) {} | |
| server.listen(port) | |
| console.log(`serving on port ${port}`) | |
| console.log(dir) |