nodeJS实现图片上传和展示

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ltt444280761/article/details/49305257

看了node初学者入门,照着文章里完成了代码,学到了很多,学会怎么把代码分开处理,代码分为四个部分,index、server、router、requestHandler

index.js

var server = require("./Server");
var router = require("./router");
var requestHandlers = require("./requestHandler");

var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;

server.start(router.route,handle);

server.js

<span style="font-size:12px;">var http = require("http");
var url = require("url");

function start(route,handle){
	function onRequest(request,response){
		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;</span>


router.js

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;


requestHandler.js

var querystring = require("querystring"),
    fs = require("fs"),
    formidable = require("formidable");

function start(response){
  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">'+
    '<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();
  form.uploadDir='tmp';
  console.log("about to parse");
  form.parse(request,function(error,fields,files){
    console.log("parsing done");
    fs.renameSync(files.upload.path,"./tmp/test.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("D:/前端/Node.js/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();
    }
  });
}

exports.start = start;
exports.upload = upload;
exports.show = show;




展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页