expressjsHow can I use Express.js to upload a file?
Express.js is a popular Node.js web application framework that can be used to upload files. The following code example demonstrates how to upload a file using Express.js.
const express = require('express');
const multer = require('multer');
const app = express();
// Set up multer to handle file uploads
const upload = multer({ dest: 'uploads/' });
// Handle POST request to upload a file
app.post('/upload', upload.single('myFile'), (req, res) => {
const file = req.file;
if (!file) {
const error = new Error('Please upload a file');
error.httpStatusCode = 400;
return next(error);
}
res.send(file);
});
app.listen(3000);
The code above creates an Express.js application and uses the Multer middleware to handle file uploads. It sets up the upload
middleware to store uploaded files in the uploads/
directory and then handles a POST request to the /upload
endpoint. The upload.single('myFile')
part of the code tells Multer to expect a single file with the name myFile
in the request. If the file is successfully uploaded, the req.file
object will contain information about the uploaded file. The code then sends the file back as the response.
The following is a list of parts used in the code example above and their explanations:
const express = require('express')
: This line imports the Express.js module.const multer = require('multer')
: This line imports the Multer module.const upload = multer({ dest: 'uploads/' })
: This line sets up the Multer middleware to store uploaded files in theuploads/
directory.upload.single('myFile')
: This line tells Multer to expect a single file with the namemyFile
in the request.req.file
: This object will contain information about the uploaded file if the upload is successful.res.send(file)
: This line sends the file back as the response.
Helpful links
More of Expressjs
- How do I use adm-zip with Express.js?
- How do I download a zip file using Express.js?
- How do I find Express.js tutorials on YouTube?
- How do I set the time zone in Express.js?
- How can I set up X-Frame-Options in ExpressJS?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js to generate a zip response?
- How can I use Express.js to yield results?
- How can I use the x-forwarded-for header in Express.js?
- How do I download a zip file using Express.js?
See more codes...