Detailed explanation of express's middleware bodyParser_node.js
WBOY
Release: 2016-05-16 16:29:07
Original
1834 people have browsed it
bodyParser is used to parse the content in the body requested by the client, and internally uses JSON encoding processing, url encoding processing and file upload processing.
Upload files to the server
function uploadFile(){
var formData=new FormData();
var files=document.getElementById("files").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);
}
Please select a file:
The above XMLHttpRequest object and FormData object are the contents of HTML5 and will not be explained in detail. These two objects can be used to upload files selected by the user to the server.
After using the app.use(express.bodyParser()) middleware on the server side, the http.IncomingMessage requested by the client, that is, the res object has a files attribute.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn