Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of https://zzagtung@github.com/zzagtung/node_st…

…udy.git

Conflicts:
	requestHandlers.js
	router.js
	server.js
  • Loading branch information...
commit 405102d84e4e6d138aed6f9ed86c316df3b27b9d 2 parents 30daf20 + 985592e
@zzagtung authored
Showing with 49 additions and 50 deletions.
  1. +2 −1  README
  2. +44 −35 requestHandlers.js
  3. +2 −2 router.js
  4. +1 −12 server.js
View
3  README
@@ -1 +1,2 @@
-node beginner study.
+The node beginner book study.
+http://www.nodebeginner.org/index-kr.html
View
79 requestHandlers.js
@@ -1,45 +1,54 @@
-var querystring = require("querystring");
- fs = require("fs");
+var querystring = require("querystring"),
+ fs = require("fs"),
+ formidable = require("formidable");
-function start(response, postData) {
- 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" method="post" enctype="multipart/form-data">'+
- '<input type="file" name="upload">'+
- '<input type="submit" value="Upload file" />'+
- '</form>'+
- '</body>'+
- '</html>';
+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" method="post" enctype="multipart/form-data">'+
+ '<input type="file" name="upload">'+
+ '<input type="submit" value="Upload file" />'+
+ '</form>'+
+ '</body>'+
+ '</html>';
- response.writeHead(200, {"Content-Type": "text/html"});
- response.write(body);
- response.end();
+ response.writeHead(200, {"Content-Type": "text/html"});
+ response.write(body);
+ response.end();
}
-function upload(response, postData) {
- 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);
+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");
+ 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();
+ });
}
-function show(response, postData) {
- console.log("Request handler 'show' was called.");
- fs.readFile("/tmp/test.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();
- }
- });
+function show(response, request) {
+ console.log("Request handler 'show' was called.");
+ fs.readFile("/tmp/test.jpg", "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/jpeg"});
+ response.write(file, "binary");
+ response.end();
+ }
+ });
}
exports.start = start;
View
4 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);
if (typeof handle[pathname] === 'function') {
- return handle[pathname](response, postData);
+ return handle[pathname](response, request);
}
else {
console.log("No request handler found for " + pathname);
View
13 server.js
@@ -3,20 +3,9 @@ var url = require("url");
function start(route, handle) {
function onReqeust(request, response) {
- var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Reqeust for " + pathname + " received.");
-
- 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);
- });
+ route(handle, pathname, response, request);
}
http.createServer(onReqeust).listen("8888");
Please sign in to comment.
Something went wrong with that request. Please try again.