Last active
August 29, 2015 14:06
-
-
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.
python 2.x:
python -m SimpleHTTPServer 1337python 3.x:
python -m http.server 1337or 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
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
| 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 |
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