coding-to-music / coding-to-music.github.io

https://pandemic-overview.readthedocs.io/en/latest/index.html
MIT License
2 stars 8 forks source link

Node.js Upload multiple images to MongoDB example using Multer, GridFsStorage and mongod #347

Open coding-to-music opened 2 years ago

coding-to-music commented 2 years ago

Node.js Upload multiple images to MongoDB example using Multer, GridFsStorage and mongod

For more detail, please visit:

How to upload/store images in MongoDB using Node.js & Express

https://github.com/bezkoder/node-js-upload-image-mongodb

Front-end Apps to work with this Node.js Server:

More Practice:

Node.js Express File Upload Rest API example (static folder)

Node.js Express File Upload with Google Cloud Storage example

Upload & resize multiple images in Node.js using Express, Multer, Sharp

Node.js, Express & MongoDb: Build a CRUD Rest Api example

Server side Pagination in Node.js with MongoDB and Mongoose

Node.js + MongoDB: User Authentication & Authorization with JWT

Associations:

MongoDB One-to-One relationship tutorial with Mongoose examples

MongoDB One-to-Many Relationship tutorial with Mongoose examples

MongoDB Many-to-Many Relationship with Mongoose examples

Integration (run back-end & front-end on same server/port)

Integrate React with Node.js Restful Services

Integrate Angular with Node.js Restful Services

Integrate Vue with Node.js Restful Services

Project setup

npm install

Run

node src/server.js

Output

Running at localhost:8080
Error: The database connection must be open to store files
    at GridFsStorage._handleFile (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/multer-gridfs-storage/lib/gridfs.js:175:12)
    at /mnt/ap/ap/node-js-upload-image-mongodb/node_modules/multer/lib/make-middleware.js:145:17
    at allowAll (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/multer/index.js:8:3)
    at wrappedFileFilter (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/multer/index.js:44:7)
    at Busboy.<anonymous> (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/multer/lib/make-middleware.js:115:7)
    at Busboy.emit (events.js:400:28)
    at Busboy.emit (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/busboy/lib/main.js:38:33)
    at PartStream.<anonymous> (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/busboy/lib/types/multipart.js:213:13)
    at PartStream.emit (events.js:400:28)
    at HeaderParser.<anonymous> (/mnt/ap/ap/node-js-upload-image-mongodb/node_modules/dicer/lib/Dicer.js:51:16) {
  storageErrors: []
}

{"message":"Error when trying upload many files: Error: The database connection must be open to store files"}

Attempt to connect to the database

localhost:8080/files

Output

{"message":"connect ECONNREFUSED 127.0.0.1:27017"}

Mongod port

localhost:27017

Output

It looks like you are trying to access MongoDB over HTTP on the native driver port.

Start the mongod database server

mongod

Go to route files

http://localhost:8080/files

Output

{"message":"No files found!"}

Upload a file

```java
http://localhost:8080/

Output

{"message":"Files have been uploaded."}

Upload a file

http://localhost:8080/files

Output

[{"name":"1640923740720-bezkoder-step5d.png","url":"http://localhost:8080/files/1640923740720-bezkoder-step5d.png"}]

Console Output

Running at localhost:8080
[
  {
    fieldname: 'file',
    originalname: 'step5d.png',
    encoding: '7bit',
    mimetype: 'image/png',
    id: new ObjectId("61ce825c48a0585965e88d45"),
    filename: '1640923740720-bezkoder-step5d.png',
    metadata: null,
    bucketName: 'photos',
    chunkSize: 261120,
    size: 118473,
    md5: undefined,
    uploadDate: 2021-12-31T04:09:00.799Z,
    contentType: 'image/png'
  }
]