formidable express
// 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:
- These dependencies were not found: * webpack/lib/web/FetchCompileWasmPlugin in ./~/worker-loader/dist/index.js
- FAILURE: Build failed with an exception react native android
- regular expression characters
- javascript find in array
- node spawn
- how to stop user from clicking outside alert box js
- node open file
- post method javascript code
- how to turn a string to a number js
- new date() javascript
- componentWillUnmount functional component for asynchronous requests
- javascript generator function
- get value of input jqueyr
- javascript setattribute onclick function with parameters
- angular firebase crud
- require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")`
- flatten nested object js
- js splice 2d array
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- javascript string interpolation
- react set port
- transpose of the matrix in javascript
- react text input onchange
- javascript prevent context menu