0

I am new to Node.js and I am building an app to that stores image file. But I am not sure what should the type of image be?

const userSchema = new mongoose.Schema({
    userImage: {
        type: String,
        required: true
    }
})
3rdsty4bl00d
  • 134
  • 10
  • 1
    https://stackoverflow.com/a/29780816/2845389 – Kaushik Apr 25 '19 at 06:21
  • 1
    i google search `img: { data: Buffer, contentType: String }` – Donald Wu Apr 25 '19 at 06:21
  • Possible duplicate of [Store an image in MongoDB using Node.js/Express and Mongoose](https://stackoverflow.com/questions/29780733/store-an-image-in-mongodb-using-node-js-express-and-mongoose) – Junius L Apr 25 '19 at 06:22

1 Answers1

0

You have multiple options.
1. Convert the image to base64 and store it as string (I would not recommend doing so, since this might be very slow for larger images)
2. Store the image as Buffer
3. Upload the image to a CDN / S3 / some other file storage and save the path to the image as string in your database (recommended for performance)

marcobiedermann
  • 4,317
  • 3
  • 24
  • 37
  • You recommend uploading image to a `CDN`. But how? Can you give some reference or website url? Thank you in advance. – 3rdsty4bl00d Apr 25 '19 at 06:31
  • Cloudinary is a pretty good and cheap CDN. https://cloudinary.com/. They provide various SKDs for client and server to upload media assets to their platform. They also optimize your assets and provide them in different file formats which is also a huge performance benefit – marcobiedermann Apr 25 '19 at 07:17
  • AWS S3 JavaScript SDK documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html – marcobiedermann Apr 25 '19 at 07:19