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
functionRoute (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