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 startedThe 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 the- uploads/directory.
- app.post('/upload', upload.single('myFile'), (req, res, next) => {...}): This line listens for POST requests on the '/upload' route and uses the- upload.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 use Express.js to parse YAML files?
- How can I use Express.js and Winston together to create a logging system?
- How do I use Express.js with W3Schools?
- How do Express.js and Spring Boot compare in terms of features and performance?
- How can I create a PNG using Express.js?
- How can I use Express.js to trace requests?
- How do I use Express.js to make an options request?
- How do I render a template using Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I use the x-forwarded-for header in Express.js?
See more codes...