Allowing users to upload files with Express is fairly straight forward using the multer node module:
npm install multerIn your server, you'll need to require multer and then create an instance of it specifying any options such as where to store the files (in the code below, called "upload"):
var multer = require('multer') var upload = multer({ dest: 'uploads/' })Then in any of your routes that you expect a file to be uploaded, you pass in "upload" and tell it what to expect. In this case, a single file that is named "photo" on the form:
app.post('/upload', upload.single('photo'), function (req, res) { console.log(req.file); res.send("uploaded: " + req.file); // req.file is the uploaded file information // req.body will hold the other text fields });Doing file uploads in HTML requires using a bit of a different "form" tag, in particular a tag that specifies an "enctype" or encoding type of "multipart/form-data". Here is an example:
<html> <body> <form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="photo"> <input type="submit" name="submitbutton" value="Submit" /> </form> </body> </head>