Multer
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
- Create new folder named node-multer
- run npm init -y
- 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


For any query mail on : parthpadhiar99@gmail.com
Follow on gitHub