formidable example
// make this a middleware function,
// then put it on the route like you used jwt,
// then get the value with req.users.
const { IncomingForm } = require('formidable')
const { resolve } = require('path')
const { existsSync, writeFileSync } = require('fs')
module.exports = (req, res, next) => {
const form = new IncomingForm({
maxFileSize: 1 * 1024 * 1024,
keepExtensions: true
})
form.parse(req, (error, fields, file) => {
if (error) return next(error)
const patternFile = /\.(jpg|jpeg|png|svg|gif|raw|webp)$/gi.test(file.productImage.name)
if (patternFile) {
const pathFile = resolve(process.cwd(), 'servers/uploads/', file.productImage.name)
const fileExits = existsSync(pathFile)
if (!fileExits) {
writeFileSync(pathFile)
req.users = JSON.parse(JSON.stringify({ fields, file }))
return next()
}
req.users = JSON.parse(JSON.stringify({ fields, file }))
return next()
}
})
}
Also in JavaScript:
- how to get duplicate values from array in javascript
- firestore update map
- create dynamic fields in react
- how to remove last index of array in javascript
- jquery enable submit button
- property 'name' does not exist on type 'eventtarget' react
- Javascript format to currency
- javascript append how first element
- compare date and time in js
- devextreme custom dxdatagrid field
- jquery OR operator
- ionic g service services/auth ionic g service services/authGuard npm i @ionic/storage npm i @auth0/angular-jwt ionic cordova plugin add cordova-sqlite-storage
- array filter
- jquery ready
- js how to know if element touch border
- difference between key and code in javascript
- font awesome react
- firebase.database is not a function
- unable to add class in jsx
- hidden jquery
- get location from brwoser react
- hoe verbind je de nodemcu 8266 met adafruit io
- js last array element
- const { message }