Created
January 6, 2017 23:18
-
-
Save amonks/d4954ebe1c86e0b6c9d2fb4c1bb7a795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env node | |
// static-server.js | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment