Created
August 23, 2014 17:28
-
-
Save thomd/8e998d7581894ec36459 to your computer and use it in GitHub Desktop.
Serve local files via HTTP
This file contains hidden or 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
// A simple static file server for development use. | |
var express = require('express'), | |
app = express(); | |
app.use('/', express.static(__dirname)); | |
app.listen('8000'); |
This file contains hidden or 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 ruby | |
# Serve the current directory via HTTP. | |
# Like Python's SimpleHTTPServer, but with no-cache headers. | |
# Default port 8000, specify alternate port as first parameter: | |
# www 3000 | |
# sudo www 80 # (probably a bad idea) | |
# from https://gist.github.com/pda/3300372 | |
require "webrick" | |
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler | |
DAY = 86400 | |
def do_GET(request, response) | |
super | |
set_no_cache(response) | |
set_content_type(response) | |
end | |
private | |
def set_no_cache(response) | |
response["Content-Type"] | |
response["ETag"] = nil | |
response["Last-Modified"] = Time.now + DAY | |
response["Cache-Control"] = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0" | |
response["Pragma"] = "no-cache" | |
response["Expires"] = Time.now - DAY | |
end | |
def set_content_type(response) | |
response["Content-Type"] = content_type(response.filename) | |
end | |
def content_type(path) | |
case path.match(%r{\.(\w+)\z})[1] | |
when "html" then "text/html" | |
when "js" then "text/javascript" | |
when "css" then "text/css" | |
else `/usr/bin/file --brief --mime-type #{Shellwords.escape(path)}`.chomp | |
end | |
end | |
end | |
WEBrick::HTTPServer.new(Port: ARGV.first || 8000).tap do |server| | |
server.mount "/", NonCachingFileHandler , Dir.pwd | |
trap("INT") { server.stop } | |
server.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment