expressjsHow can I use Express.js to post a file?
Express.js is a web application framework for Node.js that enables you to create a server-side application. You can use Express.js to post a file by using the multer
middleware.
const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer({
dest: 'uploads/'
});
app.post('/upload', upload.single('file'), (req, res) => {
console.log(req.file);
res.send('File uploaded successfully');
});
The example code above creates an Express application with the multer
middleware. The multer
middleware is used to parse the file that is uploaded. The upload.single('file')
function is used to specify the name of the file that is being uploaded. The req.file
object is used to get the information about the file that was uploaded. The res.send
function is used to send a response to the client.
Parts of the code:
const express = require('express')
: This line imports the Express module into the application.const multer = require('multer')
: This line imports the multer module into the application.app.post('/upload', upload.single('file'), (req, res) => {
: This line creates a POST route that is used to handle the file upload.upload.single('file')
: This line specifies the name of the file that is being uploaded.req.file
: This object is used to get the information about the file that was uploaded.res.send('File uploaded successfully')
: This line sends a response to the client.
Helpful links
More of Expressjs
- How can I set up unit testing for an Express.js application?
- How can I use Express.js to generate a zip response?
- How do I set the time zone in Express.js?
- How can I use express-zip js to zip and download files?
- How do I upload a file using Express.js?
- How do I use adm-zip with Express.js?
- How do I download a zip file using Express.js?
- How do I download a zip file using Express.js?
- How can I use Zipkin to trace requests in Express.js?
- How do I use Yarn to add Express.js to my project?
See more codes...