成都网站建设设计

将想法与焦点和您一起共享

nodejs上传图片并展示-创新互联

效果图

安乡ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

nodejs上传图片并展示

nodejs上传图片并展示

服务模块

   var http = require("http");

   var url = require("url");

   

   function start(route,handle){

   function onRequest(request,response){

   var pathname = url.parse(request.url).pathname;

   if (pathname != "/favicon.ico") {

   console.log("Request for" + pathname + " received");

   route(handle,pathname,response,request);

   }

   }

   

   http.createServer(onRequest).listen(8888);

   console.log("Server has started");

   }

   

   exports.start = start;

路由模块

   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;

请求处理模块

   var querystring = require("querystring"),

       fs = require("fs"),

       formidable = require("formidable");

   

   function start(response) {

     console.log("Request handler 'start' was called.");

   

     var body = ''+

       ''+

       ''+

       ''+

       ''+

       '

       'method="post">'+

       ''+

       ''+

       '

'+

       ''+

       '';

   

       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();

     console.log("about to parse");

     form.parse(request, function(error, fields, files) {

       console.log("parsing done");

       var readStream = fs.createReadStream(files.upload.path);

       var writeStream=fs.createWriteStream("./tmp/test.png");

       readStream.pipe(writeStream);

       readStream.on('end',function(){

       fs.unlinkSync(files.upload.path);

       });

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

       response.write("received image:
");

       response.write("");

       response.end();

     });

   }

   

   function show(response) {

     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();

       }

     });

   }

   

   exports.start = start;

   exports.upload = upload;

   exports.show = show;

index.js

   var server = require("./server");

   var router = require("./route");

   var requestHandlers = require("./requestHandlers");

   

   var handle = {};

   handle["/"] = requestHandlers.start;

   handle["/start"] = requestHandlers.start;

   handle["/upload"] = requestHandlers.upload;

   handle["/show"] = requestHandlers.show;

   

   server.start(router.route,handle);

访问:http://localhost:8888/start

nodejs上传图片并展示

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享题目:nodejs上传图片并展示-创新互联
当前链接:http://chengdu.cdxwcx.cn/article/ccpeoe.html