1976
1976

Reputation: 109

Image Upload without Multer in Node.js

How can I upload and store images in MongoDB without using Multer/any other middleware? Is this possible?

Upvotes: 4

Views: 11440

Answers (4)

Alex  Gor
Alex Gor

Reputation: 575

Yes, you can implement it in your code. Middleware such as Multer implements this functionality. So you can do it too. Multer implement over busboy you can check their implementation in their repositories - multer and busboy. Source code is good way for study.
Or see how pipe data from request and you can receive it in your middleware.

req.on('data', data => {
   // ... do your stuff here
});

Upvotes: 1

Archil Labadze
Archil Labadze

Reputation: 4325

You can use request data stream or pipe it to some other stream something like fs.createWriteStream, here is code example:

request.on('data', (data: any) => {
  console.log(data);
});

Hope this is what you need.

Upvotes: 1

James
James

Reputation: 130

Install npm install --save express-fileupload

In the app.js/server.js file do this

const upload = require('express-fileupload');
// Then you can set a middleware for express-fileupload
app.use(upload());

You can see the full example of how to do this here.

After doing this you can save the location(url) of your file in mongoDB for referencing it later in a web page.

To know how to access the properties of the file you are uploading eg: fileName etc... see here. Good luck

Upvotes: 0

Shams Nahid
Shams Nahid

Reputation: 6559

Definitely possible.

Instead of multer use busboy. And for file storing in mongoDB database, check GridFS

Upvotes: 0

Related Questions