Skip to content

Instantly share code, notes, and snippets.

@weiland
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save weiland/e208d4a7df0fdc6465c0 to your computer and use it in GitHub Desktop.

Select an option

Save weiland/e208d4a7df0fdc6465c0 to your computer and use it in GitHub Desktop.
Quick Static Webserver in Node.js, Python, Ruby, PHP and IIS in case one has to run a server in a local directory.

Static Webserver Collection

PHP Webserver

php -S 127.0.0.1:1337

Python Webserver

python 2.x:

python -m SimpleHTTPServer 1337

python 3.x:

python -m http.server 1337

or use Mathias Bynens function

server () {
	local port="${1:-1337}"
	sleep 1 && open "http://localhost:${port}/" &
	python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}

as a shell function to run a python webserver and open a browser window

Ruby Webserver

Webrick:

ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start'

or if you are on ruby >= 1.9.2 run:

ruby -run -ehttpd . -p1337
var path = require('path'),
fs = require('fs'),
port = parseInt( (process.argv[2] || 1337), 10 );
require('http').createServer( function( req, res ) {
var uri = require('url').parse(req.url).pathname,
filename = path.join( process.cwd(), uri );
fs.exists( filename, function( exists ) {
if( !exists ) {
res.writeHead( 404, {'Content-Type': 'text/plain'} );
res.write( '404 :( \n' );
res.end();
return;
}
if( fs.statSync(filename).isDirectory() ) {
if( fs.existsSync(filename + '/index.html') ) {
filename += '/index.html';
} else {
fs.readdir(filename, function (err, files) {
if (err) {
throw err;
}
var filesArray = [];
files.map(function (file) {
return path.join('./', file);
}).forEach(function (file) {
filesArray.push(file);
console.log("%s (%s)", file, path.extname(file));
});
res.writeHead( 200 );
res.write( filesArray.join('<br>\n') );
res.end();
});
return;
}
}
fs.readFile( filename, 'binary', function( err, file ) {
if( err ) {
res.writeHead( 500, {'Content-Type': 'text/plain'} );
res.write(err + '\n');
res.end();
return;
}
res.writeHead( 200 );
res.write( file, 'binary' );
res.end();
});
});
}).listen( port );
console.log('Webserver runs on Port %s', port);
// Alternatively, installing http-server globally will do the trick as well
// npm install -g http-server (or install node-static)
// http-server -p 1337

Windows IIS Express

server.bat (runs the server in the current directory) BEWARE: it may won't run when you do have a space in your project folder name

@echo off
"C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:%cd% /port:1337

server.bat file (with a fixed project as path parameter)

@echo off
"C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:D:\project /port:1337

(there is no configuration that index.html is a default file)

sample Web.config for the IIS

<system.webServer>
    <defaultDocument>
        <files>
            <add value="index.html" />
        </files>
    </defaultDocument>
</system.webServer>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment