Skip to content

Instantly share code, notes, and snippets.

@amonks
Created January 6, 2017 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amonks/d4954ebe1c86e0b6c9d2fb4c1bb7a795 to your computer and use it in GitHub Desktop.
Save amonks/d4954ebe1c86e0b6c9d2fb4c1bb7a795 to your computer and use it in GitHub Desktop.
#! /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