Node.js Web Server in Practice: File Upload
Preface
File upload is an essential capability in server-side development.
In Node.js web server development,
you can use formidable to implement file upload.
qiao-z-upload
You can refer to the official documentation for formidable usage.
Here is a packaged npm module for convenience: https://code.insistime.com/#/qiao-z-upload
qiao-z-upload is the upload plugin for qiao-z,
which depends on the qiao-z web server framework.
qiao-z
qiao-z is a minimalist Node.js web server framework.
See: https://qiao-z.vincentqiao.com/#/
In qiao-z, you can easily implement file upload.
1. Pass in the qiao-z-upload plugin during initialization
// options
const options = {
// upload, handles file upload requests, returns file info to req.body
upload: require('qiao-z-upload'),
};
// app
const app = qz(options);
2. Get the uploaded file information from req.body.files
/**
* upload controller
*/
module.exports = (app) => {
// upload
app.post('/upload', async (req, res) => {
console.log(req.body.files);
});
};
Summary
1. Introduction to formidable, https://www.npmjs.com/package/formidable
2. Introduction to qiao-z-upload, https://code.insistime.com/#/qiao-z-upload
3. Implementing file upload in qiao-z, https://qiao-z.vincentqiao.com/#/plugins/upload