Upload files using body-paser

  node.js, question

In uploading files using body-paser middleware, the upload was unsuccessful.
Html code is as follows
<! DOCTYPE html>
<html lang=”zh-Ch”>
<head>

<meta charset="UTF-8">
 < title > upload file < /title > to server side
 <script type="text/javascript">
 function uploadFile() {
 var formData=new FormData();
 var files=document.getElementById("file").files;
 var file=files[0];
 formData.append('myfile',file);
 var xhr=new XMLHttpRequest();
 xhr.open('POST','index.html',true);
 xhr.onload=function (e) {
 if(this.status==200)
 document.getElementById("result").innerHTML=this.response;
 };
 xhr.send(formData);
 bracket
 </script>

</head>
<body>
< h1 > upload files using bodyParser middleware < /h1 >
<form id=”form1″ enctype=”multipart/form-data”>

Please select file < inputtype = "file" id = "file" name = "file" > < br/>
 < input type="button" value= "upload file" onclick="uploadFile (); "  >

</form>
<output id=”result”></output>
</body>
</html>

Js node.js as follows:

var express=require(‘express’);
var fs=require(‘fs’);
var app=express();
var bodyParser = require(‘body-parser’);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({

extended: false

}));
app.get(‘/index.html’,function (req,res) {

Res.sendFile(__dirname plus' /index.html');

});
app.post(‘/index.html’,function (req,res) {

var file=req.files.myfile;
 fs.readFile(file.path,function (err,data) {
 If(err) res.send ('read file failed.'  );
 else{
 fs.writeFile(file.name,data,function (err) {
 If(err) res.send ('write failed.'  );
 Else res.send ('file uploaded successfully.'  );
 })
 bracket
 })

});
app.listen(1342,”127.0.0.1″);

Error tip:

TypeError: Cannot read property ‘myfile’ of undefined

at D:\Program Files\nodejs\server.js:14:20
 at Layer.handle [as handle_request] (D:\Program Files\nodejs\node_modules\express\lib\router\layer.js:95:5)
 at next (D:\Program Files\nodejs\node_modules\express\lib\router\route.js:131:13)
 at Route.dispatch (D:\Program Files\nodejs\node_modules\express\lib\router\route.js:112:3)
 at Layer.handle [as handle_request] (D:\Program Files\nodejs\node_modules\express\lib\router\layer.js:95:5)
 at D:\Program Files\nodejs\node_modules\express\lib\router\index.js:277:22
 at Function.process_params (D:\Program Files\nodejs\node_modules\express\lib\router\index.js:330:12)
 at next (D:\Program Files\nodejs\node_modules\express\lib\router\index.js:271:10)
 at urlencodedParser (D:\Program Files\nodejs\node_modules\body-parser\lib\types\urlencoded.js:95:37)
 at Layer.handle [as handle_request] (D:\Program Files\nodejs\node_modules\express\lib\router\layer.js:95:5)
 
 Node novice, still hope not stint!  !  !

body-parserFiles are not supported.

multerYou can use this.

app.use(multer({ dest: path.join(__dirname), 'public/uploadDir'}))

It is easy to look at the documents.