Nodejs upload images and show

Source: Internet
Author: User

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/8D/1B/wKioL1iHDAHgGD4rAAAfRWe64P0303.png-wh_500x0-wm_3 -wmp_4-s_4183927979.png "style=" Float:none; "title=" Qq20170124160955.png "alt=" Wkiol1ihdahggd4raaafrwe64p0303.png-wh_50 "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8D/1D/wKiom1iHDAKDUkOtAAJlVe2Q6bE449.png-wh_500x0-wm_3 -wmp_4-s_2714646750.png "style=" Float:none; "title=" Qq20170124161009.png "alt=" Wkiom1ihdakdukotaajlve2q6be449.png-wh_50 "/>

Service Modules

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;

Routing module

Function route (handle,pathname,response,request) {

Console.log ("about-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;

Request Processing Module

var querystring = require ("querystring"),

FS = require ("FS"),

Formidable = require ("formidable");


function Start (response) {

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


var body = '

'

' <meta http-equiv= ' Content-type "content=" text/html; ' +

' Charset=utf-8 '/> ' +

'

' <body> ' +

' <form action= '/upload "enctype=" multipart/form-data "' +

' method= ' post > ' +

' <input type= ' file ' name= ' upload ' multiple= ' multiple ' > ' +

' <input type= ' submit ' value= ' Upload file '/> ' +

' </form> ' +

' </body> ' +

'


Response.writehead ($, {"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 ($, {"Content-type": "Text/html"});

Response.Write ("received image:<br/>");

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 ($, {"Content-type": "Text/plain"});

Response.Write (Error + "\ n");

Response.End ();

} else {

Response.writehead ($, {"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);

Visit: Http://localhost:8888/start


This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1894091

Nodejs upload images and show

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.