Skip to content

Instantly share code, notes, and snippets.

@amonks
Created December 27, 2016 04:23
Show Gist options
  • Save amonks/947e001923c480afe3883aa5a75ed8d7 to your computer and use it in GitHub Desktop.
Save amonks/947e001923c480afe3883aa5a75ed8d7 to your computer and use it in GitHub Desktop.

installation:

$ npm install --save express

usage

option 1

$ node ./static-server.js [port]

(default port is 3000)

option 2

$ 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment