formidable-express-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

formidable-express-middleware

Background

Let formidable has ability of insert HTML Form Data such as user_id, user_name in the customized filename.

How to use?

import the library

import FormidableMiddleware,{IFormMiddleWareRequestTemplate} from 'formidable-express-middleware';

Create the HTML Form Data Data Type:

interface IFormMiddleWareFields{
    content:string
}

Create the Middleware Instance

const uploadDir = 'uploads';
const formidableMiddleware = FormidableMiddleware({
      /*formidable options*/
      uploadDir,
      keepExtensions: true,
      maxFileSize: 200 * 1024 ** 2,
      filter: (part) => part.mimetype?.startsWith('image/') || false,
})

Create a Express.js Web Service which use formidable, and insert the middleware in the function.

app.post("/memo", 
formidableMiddleware<IFormMiddleWareFields>(
    "image" /*file upload HTML element name*/, 
    ["content"] /*Form Data Names which are added in filename*/),

    async (req: Request, res: Response) => {
        const memos = [];
        memos.push({
            content: req.fields.content,
            image: req.file.newFilename
        });
        res.json(memos);
    }
});

How to test?

Please run following command under terminal under "test" folder

npx ts-node server.ts

Please run Insomnia/Postman testing following service:

http://localhost:8080/test

Method: POST
Required Fields:
content (plain text)
image (image file)

Package Sidebar

Install

npm i formidable-express-middleware

Weekly Downloads

1

Version

1.1.0

License

ISC

Unpacked Size

5.67 kB

Total Files

4

Last publish

Collaborators

  • small_it_potatoes