formidable form node js

// 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()
    }
  })
}

3
2

                                    const http = require('http');const formidable = require('formidable'); const server = http.createServer((req, res) => {  if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {    // parse a file upload    const form = formidable({ multiples: true });     form.parse(req, (err, fields, files) => {      res.writeHead(200, { 'content-type': 'application/json' });      res.end(JSON.stringify({ fields, files }, null, 2));    });     return;  }   // show a file upload form  res.writeHead(200, { 'content-type': 'text/html' });  res.end(`    <h2>With Node.js <code>"http"</code> module</h2>    <form action="/api/upload" enctype="multipart/form-data" method="post">      <div>Text field title: <input type="text" name="title" /></div>      <div>File: <input type="file" name="multipleFiles" multiple="multiple" /></div>      <input type="submit" value="Upload" />    </form>  `);}); server.listen(8080, () => {  console.log('Server listening on http://localhost:8080/ ...');});

3 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
formidable example using formidable with express use of formidable js use of formidable formidable npm express using express formidable formidable docs formidable with express formidable js example javascript formidable formidable node js docs formidable npmjs formidable documentation how to use formidable in node js with express use /" in formidable form formidable express npm formidable express js formidable np, npm formidable tutorial express and formidable import formidable from formidable formidable node.js formidable form api node formidable module tutorial const formidable = require("formidable"); formidable express nodejs formidable options formidable with react and node js what is formidable used for in node js formidable express what is formidable in node js formidable express.js formidable in node js formidable node formidable api node js use of formidable in nodejs nodejs spawn example bcrypt nodejs example node js Promise example using express-formidable in node js form formidable data node js form data formidable express js formidable events formidable keepExtensions waht does keepextensions do in formidable nodejs form data handling in nodejs with formidable formidable nodejs example formidable library node js formidable js formidable formitable npm formidable npm cuts files formidable npm cuts filed node js formadable nodejs form to file formidable node upload url formindable js expressformidable upload image formidable package formidable upload file nodejs formidable file upload formidable express upload formidable npm UnhandledPromiseRejectionWarning formidable install path node formidable formidable module in nodejs file upload with express with formidable formidable in reactjs new formidable.IncomingForm() formidable file upload express formidable file upload should i use formidable in node server formidable get form data formidable module npm package formidable formidable npm formidable nom html formidable express how to upload file in nodejs with formidable formidable npm documentation formidable upload formidable nodeks formidable ndoejs form.on formidable npm npm formidable setup formidable in react read file with formidable javascript handle uploaded data formidable formidable;e node js formidab;e node js formidable nodejs nodejs formidable formidable nodejs formidable express example formidable tutorial node formidable js formidable node js formidibale file upload recieve incoming form without formidable formidable form node js
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source