Created
November 2, 2012 22:55
-
-
Save adparadise/4004854 to your computer and use it in GitHub Desktop.
Proxy server, serving a local file
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 mime = require('mime'); | |
function serveLocalFile (httpRequest, httpResponse, filename) { | |
console.log("local: " + httpRequest.url); | |
var send404 = function () { | |
httpResponse.setHeader('Content-Type', 'text/html'); | |
httpResponse.write("NOT FOUND: " + httpRequest.url); | |
httpResponse.end(); | |
}; | |
var existsCallback = function (exists) { | |
var readStream; | |
if (!exists) { | |
send404(); | |
return; | |
} | |
httpResponse.setHeader('Content-Type', | |
mime.lookup(httpRequest.url)); | |
readStream = fs.createReadStream(filename); | |
readStream.pipe(httpResponse); | |
}; | |
fs.exists(filename, existsCallback); | |
}; |
No, this is designed to run in Node.js.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work in-browser with HTML?