expressjsHow can I use Express.js to upload a file?
Express.js is a web application framework for Node.js that can be used for uploading files. To use Express.js to upload a file, you will need to install the Express.js package and create an Express.js application.
Example code
const express = require('express');
const app = express();
const multer = require('multer');
const upload = multer({dest: 'uploads/'});
app.post('/upload', upload.single('myFile'), (req, res, next) => {
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, () => console.log('Server started'))
Output example
Server started
The code above will create an Express.js application that will listen for POST requests on the '/upload' route. The multer package is used to handle the file upload. The upload.single()
method is used to upload a single file, and the file is stored in the uploads/
directory. The file is then sent in the response.
Code explanation
const express = require('express');
: This line imports the Express.js package.const app = express();
: This line creates the Express.js application.const multer = require('multer');
: This line imports the multer package.const upload = multer({dest: 'uploads/'});
: This line configures multer to store the uploaded file in theuploads/
directory.app.post('/upload', upload.single('myFile'), (req, res, next) => {...})
: This line listens for POST requests on the '/upload' route and uses theupload.single()
method to upload a single file.res.send(file)
: This line sends the file in the response.
Helpful links
More of Expressjs
- How do I manage user roles in Express.js?
- How do I use adm-zip with Express.js?
- How can I use the x-forwarded-for header in Express.js?
- How do I download a zip file using Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I create a quiz using Express.js?
- What is Express.js?
- How do I set the time zone in Express.js?
- How can I use Zipkin to trace requests in Express.js?
- How do I use Zod with Express.js?
See more codes...