Last active
October 29, 2019 11:56
-
-
Save joshtrigger/2e31bc4d61611341416ef929e0e98c72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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