1.代码:
1-1: 入口文件: index.js
1 var server = require('./server'); 2 var router = require("./router"); 3 var requestHandlers = require("./requestHandlers"); 4 5 var handle = {}; 6 handle["/"] = requestHandlers.start; 7 handle["/start"] = requestHandlers.start; 8 handle["/upload"] = requestHandlers.upload; 9 handle["/show"] = requestHandlers.show; 10 11 server.start(router.route, handle);
1-2: 服务器文件: server.js
1 var http = require("http"); 2 var url = require("url"); 3 4 5 function start(route, handle){ 6 http.createServer(function(request, response) { 7 8 // console.log(url.parse(request.url)); 9 // console.log(url.parse(request.url).query); 10 var pathname = url.parse(request.url).pathname; 11 console.log('request for [' +pathname+ "] received."); 12 13 //接收请求数据: 14 // var postData = ""; 15 // request.setEncoding("utf8"); 16 17 18 // request.on("data", function(chunk){ 19 // postData += chunk; 20 // console.log("Received POST data chunk '" +chunk+"'"); 21 // }); 22 23 // request.on("end", function(){ 24 // route(handle, pathname, response, postData); 25 // }); 26 27 route(handle, pathname, response, request); 28 29 30 }).listen(8888); 31 32 console.log("server has started."); 33 } 34 35 exports.start = start;
1-3: 路由文件: router.js
1 function route(handle, pathname, response, request){ 2 console.log("About to route a request for " +pathname); 3 4 if (typeof handle[pathname] === 'function') { 5 //调用函数: 6 handle[pathname](response, request); 7 } else{ 8 console.log("No request handler found for " + pathname); 9 response.writeHead(404, {"Content-Type": "text/plain"}); 10 response.write("404 Not found"); 11 response.end(); 12 } 13 } 14 15 exports.route = route;
1-4: 请求处理程序: requestHandles.js
1 var querystring = require("querystring"); 2 var url = require("url"); 3 var fs = require("fs"); 4 var formidable = require("formidable"); 5 6 function start(response) { 7 console.log("Request handler 'start' was called."); 8 9 var body = '<html>'+ 10 '<head>'+ 11 '<meta http-equiv="Content-Type" '+ 12 'content="text/html; charset=UTF-8" />'+ 13 '</head>'+ 14 '<body>'+ 15 '<form action="/upload" enctype="multipart/form-data" '+ 16 'method="post">'+ 17 '<input type="file" name="upload">'+ 18 '<input type="submit" value="Upload file" />'+ 19 '</form>'+ 20 '</body>'+ 21 '</html>'; 22 23 response.writeHead(200, {"Content-Type": "text/html"}); 24 response.write(body); 25 response.end(); 26 } 27 28 29 30 function upload(response, request) { 31 console.log("Request handler 'upload' was called."); 32 33 var form = new formidable.IncomingForm(); 34 console.log("about to parse"); 35 form.parse(request, function(error, fields, files){ 36 console.log("parsing done"); 37 38 var uploadDir = "C:/Users/dc5yy/Desktop/upload/"; 39 fs.renameSync(files.upload.path, uploadDir + files.upload.name); 40 41 response.writeHead(200, {"Content-Type": "text/html"}); 42 response.write("received image:<br />"); 43 response.write("<img width='400px' src='/show?filename=" +files.upload.name+ "' />"); 44 response.end(); 45 46 }); 47 48 } 49 50 51 function show(response, request){ 52 console.log("Request handle 'show' was called."); 53 var uploadDir = "C:/Users/dc5yy/Desktop/upload/"; 54 var filename = uploadDir + url.parse(request.url, true).query.filename; 55 console.log(filename); 56 fs.readFile(filename, "binary", function(error, file){ 57 if (error) { 58 response.writeHead(500, {"Content-Type": "text/plain"}); 59 response.write(error + " "); 60 response.end(); 61 } else{ 62 response.writeHead(200, {"Content-Type": "image/jpg"}); 63 response.write(file, "binary"); 64 response.end(); 65 } 66 }); 67 } 68 69 exports.start = start; 70 exports.upload = upload; 71 exports.show = show;
2. 运行:
2-1: 上传文件目录:
2-2: 开启服务器:
2-3: 上传图片:
选择一张图片:
结果:
查看 上传目录: