Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of https://zzagtung@github.com/zzagtung/node_st…
…udy.git

Conflicts:
	requestHandlers.js
	router.js
	server.js
  • Loading branch information
zzagtung committed Dec 14, 2011
2 parents 30daf20 + 985592e commit 405102d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
3 changes: 2 additions & 1 deletion README
@@ -1 +1,2 @@
node beginner study. The node beginner book study.
http://www.nodebeginner.org/index-kr.html
79 changes: 44 additions & 35 deletions requestHandlers.js
@@ -1,45 +1,54 @@
var querystring = require("querystring"); var querystring = require("querystring"),
fs = require("fs"); fs = require("fs"),
formidable = require("formidable");


function start(response, postData) { function start(response, request) {
console.log("Request handler 'start' was called."); console.log("Request handler 'start' was called.");
var body = '<html>'+ var body = '<html>'+
'<head>'+ '<head>'+
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+ '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+
'</head>'+ '</head>'+
'<body>'+ '<body>'+
'<form action="/upload" method="post" enctype="multipart/form-data">'+ '<form action="/upload" method="post" enctype="multipart/form-data">'+
'<input type="file" name="upload">'+ '<input type="file" name="upload">'+
'<input type="submit" value="Upload file" />'+ '<input type="submit" value="Upload file" />'+
'</form>'+ '</form>'+
'</body>'+ '</body>'+
'</html>'; '</html>';


response.writeHead(200, {"Content-Type": "text/html"}); response.writeHead(200, {"Content-Type": "text/html"});
response.write(body); response.write(body);
response.end(); response.end();
} }


function upload(response, postData) { function upload(response, request) {
console.log("Request handler 'upload' was called."); console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain", "Content-Encoding":"utf8"});
response.write("You've sent: " + querystring.parse(postData).text); var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
fs.renameSync(files.upload.path, "/tmp/test.jpg");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end(); response.end();
});
} }


function show(response, postData) { function show(response, request) {
console.log("Request handler 'show' was called."); console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file) { fs.readFile("/tmp/test.jpg", "binary", function(error, file) {
if (error) { if(error) {
response.writeHead(500, {"Content-Type": "text/plain"}); response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n"); response.write(error + "\n");
response.end(); response.end();
} else { } else {
response.writeHead(200, {"Content-Type": "image/png"}); response.writeHead(200, {"Content-Type": "image/jpeg"});
response.write(file, "binary"); response.write(file, "binary");
response.end(); response.end();
} }
}); });
} }


exports.start = start; exports.start = start;
Expand Down
4 changes: 2 additions & 2 deletions router.js
@@ -1,7 +1,7 @@
function route(handle, pathname, response, postData) { function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname); console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') { if (typeof handle[pathname] === 'function') {
return handle[pathname](response, postData); return handle[pathname](response, request);
} }
else { else {
console.log("No request handler found for " + pathname); console.log("No request handler found for " + pathname);
Expand Down
13 changes: 1 addition & 12 deletions server.js
Expand Up @@ -3,20 +3,9 @@ var url = require("url");


function start(route, handle) { function start(route, handle) {
function onReqeust(request, response) { function onReqeust(request, response) {
var postData = "";
var pathname = url.parse(request.url).pathname; var pathname = url.parse(request.url).pathname;
console.log("Reqeust for " + pathname + " received."); console.log("Reqeust for " + pathname + " received.");

route(handle, pathname, response, request);
request.setEncoding("utf8");

request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '" + postDataChunk + "'.");
});

request.addListener("end", function() {
route(handle, pathname, response, postData);
});
} }


http.createServer(onReqeust).listen("8888"); http.createServer(onReqeust).listen("8888");
Expand Down

0 comments on commit 405102d

Please sign in to comment.