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 namemyFilein 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 Express.js to parse YAML files?
- How can I set up X-Frame-Options in ExpressJS?
- How do I download a zip file using Express.js?
- How can I disable the X-Powered-By header in Express.js?
- How can I use Express.js and Vite together for software development?
- How do Express.js and Spring Boot compare in terms of features and performance?
- How do I use Express.js and Yarn together in a software development project?
- How can I make an XHR request using Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I configure Express.js to use Nginx as a reverse proxy?
See more codes...