Created
October 29, 2011 12:37
-
-
Save oscarrenalias/1324399 to your computer and use it in GitHub Desktop.
A very simple Node.js static file server, based on lightnode (https://github.com/ngspinners/lightnode)
This file contains 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 very simple Node.js static file server and Ajax proxy, based on lightnode (https://github.com/ngspinners/lightnode) | |
// | |
// Requirements: | |
// Node.js | |
// npm for the installation of modules | |
// lightnode (installed via npm) | |
// | |
// How to use, with all dependencies in place: | |
// node nodeserver.js [/path/to/the/root/folder] | |
// | |
// Go to a URL such as http://localhost:8080/somefile.html where somefile.html exists in the current folder | |
// where node was started, or in the folder provided as a parameter | |
// | |
// For proxy requests, use http://localhost:8080/proxy/http://www.yourserver.com/ajaxCall to proxy your Ajax | |
// calls through this server. Please see also gist https://gist.github.com/1324360 for the same standalone | |
// proxy. | |
// | |
// This server is useful for running Ajax-enabled applications in your local workstation when your files are | |
// being served from a local server but calling the service remotely (which wouldn't work because of cross-domain | |
// Ajax security) | |
// | |
// library dependencies | |
var http = require('http'), | |
lightnode = require('lightnode'), | |
url = require('url'); | |
// default port | |
var port = 8080; | |
var server = new http.Server(); | |
server.listen(port) | |
// Create the server; optionally, provide a folder that defines which folder is served, otherwise | |
// serve the current folder | |
if(process.argv[2] != undefined) | |
folder = process.argv[2] | |
else | |
folder = "." | |
console.log("Serving folder: " + folder) | |
console.log("Listening in port: " + port); | |
var website = new lightnode.FileServer(folder) | |
server.addListener('request', function(request, response) { | |
console.log("request = " + request.url); | |
if(request.url.substring(0, "/proxy/".length) == "/proxy/") { | |
// handle this as a proxy request | |
/*target = request.url; | |
if(target[0] == "/") // remove the leading forward slash*/ | |
target = request.url.substring("/proxy/".length, request.url.length); | |
console.log("Proxy request received. Target: " + target); | |
// parse the url | |
url_parts = url.parse(target); | |
if(url_parts.host == undefined) { // stop processing, URL must contain http(s):// | |
response.write("ERROR: missing host in target URL " + target); | |
response.end(); | |
} | |
else { | |
var proxy = http.createClient(80, url_parts.host) | |
var proxy_request = proxy.request(request.method, url_parts.href, request.headers); | |
console.log("Creating proxy request to server: " + url_parts.hostname + ", path: " + url_parts.pathname); | |
proxy_request.addListener('response', function (proxy_response) { | |
proxy_response.addListener('data', function(chunk) { | |
response.write(chunk, 'binary'); | |
}); | |
proxy_response.addListener('end', function() { | |
response.end(); | |
}); | |
response.writeHead(proxy_response.statusCode, proxy_response.headers); | |
}); | |
request.addListener('data', function(chunk) { | |
proxy_request.write(chunk, 'binary'); | |
}); | |
request.addListener('end', function() { | |
proxy_request.end(); | |
}); | |
} | |
} | |
else { | |
console.log("Server request received:" + request.url); | |
website.receiveRequest(request, response) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment