node. js Formidable upload file implementation

Source: Internet
Author: User

This case comes from the node. JS Starter Book:

Http://www.nodebeginner.org/index-zh-cn.html

The example can only upload and display PNG pictures, of course, other files are feasible, you can fine-tune.

Install node. JS:

# CURL-SL Https://rpm.nodesource.com/setup | Bash-# yum install-y nodejs

Firewall open 8888 port;

node. JS itself is cumbersome to handle uploading files, and can be easily handled using third-party node-formidable:

NPM Install formidable

Here's the code:

Server side:

#server. js
varHTTP = require ("http");varurl = require ("url");varorder = 1;//Number of requestsfunctionstart (route, handle) {functiononrequest (Request, response) {varPostData = ""; varpathname = Url.parse (request.url). Pathname;//Request LocationConsole.log ("Request for" + Pathname + "received."); Route (handle, pathname, response, request); //Route this requestConsole.log ("Reauest received ~ +" + order);//Number of requestsorder++; } http.createserver (ONrequest). Listen (8888);//listening on port 8888Console.log ("Server has started");} Exports.start= start;

Routing:

#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);//execute Request Processing method}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;

Handler:

#requestHandlers. js
varQueryString = Require ("querystring");varFormidable = require ("Formidable");varFS = require ("FS");functionStart (response) {Console.log ("Request handler ' start ' was called."); varBODY = ' ; Response.writehead (200, { "Content-type": "Text/html" }); Response.Write (body); Response.End ();}functionUpload (response, request) {Console.log ("Request handler ' upload ' was called."); varform =NewFormidable. Incomingform (); Console.log ("About-to-parse"); Form.parse (Request,function(Error, fields, files) {Console.log ("Parsing Done"); Fs.renamesync (Files.upload.path,"/tmp/test.png");//renamedResponse.writehead (200, { "Content-type": "Text/html" }); Response.Write ("Received image:<br/>"); Response.Write ("//call the Show method to show a pictureResponse.End (); });}functionShow (response) {varImagePath = "/tmp/test.png"; Console.log ("Request handler ' show ' was called."); Fs.readfile (ImagePath,"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;

Entry file:

#index. js

var
// server var// Route var// request processor var handle = {};handle["/"] = handle["/start"] = requesthandlers.start;handle["/upload"] = requesthandlers.upload;handle["/show"] =// initial Function-parameter is the route method and corresponding processing method

Throw these into a folder and start:

# node Index.js

Browser access: http://yourhost:8888 or after adding/start to the upload page.

End.

node. js Formidable upload file implementation

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.