Multer

Parth Padhiar
2 min readNov 27, 2020

--

Nodejs- multer for image upload to mongoose database

Multer is a node.js middle ware for handling multipart/form-data, which is primarily used for uploading files. It is written on top of busboy for maximum efficiency.

Installation

$ npm install --save multer

Let’s start

  1. Create new folder named node-multer
  2. run npm init -y
  3. install express, body-parser, multer, mongoose
$ npm i express body-parser multer mongoose

4. Create new file name app.js and Import all things

5. Create Schema and model

6. Now create api for example “Hello world”

7. Now, let’s add storage option, where image will save. In here, ‘./uploads/’ means image will store in uploads folder and filename will be userPost + original filename.

8. add api for image upload. Here upload.single(‘image’) means we can upload only one file at a time. instead of single we can also use upload.any()

9. now sum all of this and your app is ready.

10. Run code nodemon or npm app.js and in Postman select POST and run localhost:3000/postImage

1.
2. Output

For any query mail on : parthpadhiar99@gmail.com

Follow on gitHub

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response