一:代码:
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 28 }).listen(8888); 29 30 console.log("server has started."); 31 } 32 33 exports.start = start;
1.3 路由文件: router.js
1 function route(handle, pathname, response, postData){ 2 console.log("About to route a request for " +pathname); 3 4 if (typeof handle[pathname] === 'function') { 5 //调用函数: 6 handle[pathname](response, postData); 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 fs = require("fs"); 3 4 function start(response, postData) { 5 console.log("Request handler 'start' was called."); 6 7 var body = '<html>'+ 8 '<head>'+ 9 '<meta http-equiv="Content-Type" content="text/html; '+ 10 'charset=UTF-8" />'+ 11 '</head>'+ 12 '<body>'+ 13 '<form action="/upload" method="post">'+ 14 '<input type="text" value="" name="userName"/>'+ 15 '<textarea name="text" rows="20" cols="60"></textarea>'+ 16 '<input type="submit" value="Submit text" />'+ 17 '</form>'+ 18 '</body>'+ 19 '</html>'; 20 21 response.writeHead(200, {"Content-Type": "text/html"}); 22 response.write(body); 23 response.end(); 24 } 25 26 27 function upload(response, postData) { 28 console.log("Request handler 'upload' was called."); 29 response.writeHead(200, {"Content-Type": "text/plain"}); 30 response.write("You've sent allData: " +querystring.parse(postData)+ " "); 31 response.write("You've sent userName: " +querystring.parse(postData).userName+ " "); 32 response.write("You've sent text: " +querystring.parse(postData).text+ " "); 33 response.end(); 34 } 35 36 37 function show(response, postData){ 38 console.log("Request handle 'show' was called."); 39 var filename = "C:/Users/dc5yy/Desktop/图片_1/06.jpg"; 40 fs.readFile(filename, "binary", function(error, file){ 41 if (error) { 42 response.writeHead(500, {"Content-Type": "text/plain"}); 43 response.write(error + " "); 44 response.end(); 45 } else{ 46 response.writeHead(200, {"Content-Type": "image/jpg"}); 47 response.write(file, "binary"); 48 response.end(); 49 } 50 }); 51 } 52 53 exports.start = start; 54 exports.upload = upload; 55 exports.show = show;
1.5 结果:
1)要读取的图片 地址:
var filename = "C:/Users/dc5yy/Desktop/图片_1/06.jpg";
2) 启动服务器:
3) http访问: