Service-Side code:
1 varExpress = require (' Express ');2 varSwig = require (' Swig '));3 //1. Introduction of Multer module4 varMulter = require (' Multer '));5 varFS = require (' FS ');6 varPath = require (' path ');7 varRedis = require ('./modules/redis '));8 9 varApp =Express ();Ten App.use (Express.bodyparser ()); One //set Swig template method; AApp.engine (' HTML ', swig.renderfile); -App.set (' View engine ', ' HTML '); -App.set (' views ', __dirname + '/views '); the - //Using Multer for file uploads. - //2, configuration multer middleware parameters; -App.use (Multer ({dest: './uploads/')})); + //3, set the route parameters, the principle is through the 2nd step in the middleware, Req.files will bring the uploaded picture information; -App.post ('/form ',function(req,res) { + A //Console.log (req.files.image); Uploaded file Information at //Console.log (req.body); - //Console.log (req.files); - - varDes_file = __dirname + "/uploads/" +Req.files.image.originalFilename; - Console.log (__dirname); - Console.log (req.files.image.originalFilename); in Console.log (des_file); - to //4, the picture is uploaded, is saved in the memory path (personal understanding, it is important, let alone understand); +Fs.readfile (Req.files.image.path,function(err, data) { - console.log (data) the * //5, through the FS module to read the memory path saved by the picture, and the picture read out the information, save to the set up directory; $Fs.writefile (des_file, data,function(err) {Panax Notoginseng if(err) { - Console.log (err); the}Else{ +Response = { AMessage: ' File uploaded successfully ', the Filename:req.files.image.originalFilename + }; - } $ //Console.log (response); $ Res.end (json.stringify (response)); - }); - }); the }); - Wuyi theApp.post ('/',function(req,res) { - if(! (req.body.owner&&req.body.type&&req.body.content)) { Wu if(req.body.type&& (["Male", "female"].indexof (req.body.type) = = = 1)){ - returnRes.json ({code:0,msg: "type Error"}) About } $ returnRes.json ({code:0,msg: "Incomplete information"}) - } -Redis.Throw(Req.body,function(Result) { - Res.json (result); A }) + }); the -App.get ('/',function(req,res) { $ Console.log (req.query); the if(!Req.query.user) { the returnRes.json ({code:0,msg: "Incomplete information"}); the } the if(Req.query.type && (["Male", "female"].indexof (req.query.type) = = = 1)){ - returnRes.json ({code:0,msg: "type Error"}); in } theRedis.pick (Req.query,function(Result) { the Res.json (result); About }) the }); theApp.get ('/form ',function(req,res) { theRes.render (' form '); + }); - the //require ("./test/test.js"). B ();Bayi // the // the //require ("./test/test.js"). A (); - //var cc = require ("./test/test"); - //cc.b (); the //cc.a (); the theApp.listen (3000);
Front-end Form form code:
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Document</title>6 </Head>7 <Body>8 <!--If you upload a picture, you must take this attribute enctype= "Multipart/form-data" -9 <formAction=""Method= "POST"enctype= "Multipart/form-data">Ten <inputtype= "text"name= "Owner"value= "123131414"/> One <inputtype= "File"name= "image"/> A <inputtype= "Submit"value= "Upload file" /> - </form> - </Body> the </HTML>
Nodejs upload image module approach;