Skip to content

Instantly share code, notes, and snippets.

@joshtrigger
Last active October 29, 2019 11:56
Show Gist options
  • Save joshtrigger/2e31bc4d61611341416ef929e0e98c72 to your computer and use it in GitHub Desktop.
Save joshtrigger/2e31bc4d61611341416ef929e0e98c72 to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
const app = express()
const upload = require('./multer')
const cloudinary = require('./cloudinary')
const fs = require('fs');
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
app.use('/upload-images', upload.array('image'), async (req, res) => {
const uploader = async (path) => await cloudinary.uploads(path, 'Images');
if (req.method === 'POST') {
const urls = []
const files = req.files;
for (const file of files) {
const { path } = file;
const newPath = await uploader(path)
urls.push(newPath)
fs.unlinkSync(path)
}
res.status(200).json({
message: 'images uploaded successfully',
data: urls
})
} else {
res.status(405).json({
err: `${req.method} method not allowed`
})
}
})
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment