Created
January 17, 2012 15:23
-
-
Save armyofda12mnkeys/1627042 to your computer and use it in GitHub Desktop.
simple request handler/router server (requires formidable module installed)
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 server = require("./server"); | |
var router = require("./router"); | |
var requestHandlers = require("./requestHandlers"); | |
var handle = {} | |
handle["/"] = requestHandlers.start1; | |
handle["/start1"] = requestHandlers.start1; | |
handle["/upload1"] = requestHandlers.upload1; | |
handle["/start2"] = requestHandlers.start2; | |
handle["/upload2"] = requestHandlers.upload2; | |
handle["/start"] = requestHandlers.start; | |
handle["/upload"] = requestHandlers.upload; | |
handle["/show"] = requestHandlers.show; | |
server.start(router.route, handle); |
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 querystring = require("querystring"); | |
var formidable = require('formidable'); | |
var util = require('util');//'sys' can be called 'util' now | |
var fs = require("fs"); | |
//If you would like to prove that an expensive operation behind /start will no longer block requests for /upload from answering immediately | |
function start1(response, request) { | |
console.log("Request handler 'start1' was called."); | |
var body = '<html>'+ | |
'<head>'+ | |
'<meta http-equiv="Content-Type" content="text/html; '+ | |
'charset=UTF-8" />'+ | |
'</head>'+ | |
'<body>'+ | |
'<form action="/upload1" method="post">'+ | |
'<textarea name="text" rows="20" cols="60"></textarea>'+ | |
'<input type="submit" value="Submit text" />'+ | |
'</form>'+ | |
'</body>'+ | |
'</html>'; | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(body); | |
response.end(); | |
} | |
function upload1(response, request) { | |
var postData=""; | |
request.setEncoding("utf8"); | |
request.addListener("data", function(postDataChunk) { | |
postData += postDataChunk; | |
console.log("Received POST data chunk '"+ | |
postDataChunk + "'."); | |
}); | |
request.addListener("end", function() { | |
console.log("Request handler 'upload1' was called."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("You've sent postData: " + postData); | |
response.write("You've sent text input: " + querystring.parse(postData).text ); | |
response.end(); | |
}); | |
} | |
function start2(response, request) { | |
console.log("Request handler 'start2' was called."); | |
var body = '<html>'+ | |
'<head>'+ | |
'<meta http-equiv="Content-Type" content="text/html; '+ | |
'charset=UTF-8" />'+ | |
'</head>'+ | |
'<body>'+ | |
'<form action="/upload2" enctype="multipart/form-data" '+ | |
'method="post">'+ | |
'<input type="text" name="title"><br>'+ | |
'<input type="file" name="upload" multiple="multiple"><br>'+ | |
'<input type="submit" value="Upload">'+ | |
'</form>'+ | |
'</body>'+ | |
'</html>'; | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(body); | |
response.end(); | |
} | |
function upload2(response, request) { | |
console.log("Request handler 'upload2' was called."); | |
var form = new formidable.IncomingForm(); | |
form.parse(request, function(err, fields, files) {//binds its own thing to request.end (so can't use request.end and this as I thought before) | |
response.writeHead(200, {'content-type': 'text/plain'}); | |
response.write('***received upload:\n\n'); | |
response.end(util.inspect({fields: fields, files: files})); | |
}); | |
} | |
function start(response, request) { | |
console.log("Request handler 'start' was called."); | |
var body = '<html>'+ | |
'<head>'+ | |
'<meta http-equiv="Content-Type" content="text/html; '+ | |
'charset=UTF-8" />'+ | |
'</head>'+ | |
'<body>'+ | |
'<form action="/upload" enctype="multipart/form-data" '+ | |
'method="post">'+ | |
'<input type="file" name="upload" multiple="multiple">'+ | |
'<input type="submit" value="Upload file" />'+ | |
'</form>'+ | |
'</body>'+ | |
'</html>'; | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(body); | |
response.end(); | |
} | |
function upload(response, request) { | |
console.log("Request handler 'upload' was called."); | |
var form = new formidable.IncomingForm(); | |
console.log("about to parse"); | |
form.parse(request, function(error, fields, files) { | |
console.log("parsing done"); | |
console.log("FILES:"+ files.upload.path ); //util.inspect({files: files}) | |
fs.renameSync(files.upload.path, "tmp/test1.png"); | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write("received image:<br/>"); | |
response.write("<img src='/show' />"); | |
response.end(); | |
}); | |
} | |
function show(response) { | |
console.log("Request handler 'show' was called."); | |
fs.readFile("tmp/test1.png", "binary", function(error, file) { | |
if(error) { | |
response.writeHead(500, {"Content-Type": "text/plain"}); | |
response.write(error + "\n"); | |
response.end(); | |
} else { | |
response.writeHead(200, {"Content-Type": "image/png"}); | |
response.write(file, "binary"); | |
response.end(); | |
} | |
}); | |
} | |
exports.start1 = start1; | |
exports.upload1 = upload1; | |
exports.start2 = start2; | |
exports.upload2 = upload2; | |
exports.start = start; | |
exports.upload = upload; | |
exports.show = show; |
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
function route(handle, pathname, response, request) { | |
console.log("About to route a request for " + pathname); | |
if (typeof handle[pathname] === 'function') { | |
handle[pathname](response, request); | |
} else { | |
console.log("No request handler found for " + pathname); | |
response.writeHead(404, {"Content-Type": "text/plain"}); | |
response.write("404 Not found"); | |
response.end(); | |
} | |
} | |
exports.route = route; |
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 http = require("http"); | |
var url = require("url"); | |
function start(route, handle) { | |
function onRequest(request, response) { | |
var postData = ""; | |
var pathname = url.parse(request.url).pathname; | |
console.log("Request for " + pathname + " received."); | |
route(handle, pathname, response, request); | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment