Created
November 5, 2019 20:43
-
-
Save mfyz/1f3628acde30375b7b7fed04ed4a904e 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 path = require('path'); | |
const multer = require('multer'); | |
const cloudinary = require('cloudinary'); | |
const PORT = process.env.PORT || 4004; | |
const storage = multer.diskStorage({ | |
destination: function (req, file, cb) { | |
cb(null, 'uploads/') | |
}, | |
filename: function (req, file, cb) { | |
cb(null, file.originalname); | |
} | |
}) | |
const upload = multer({ storage: storage }); | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.get('/', (req, res) => { | |
cloudinary.v2.api.resources((error, result) => { | |
let html = '<form method="post" enctype="multipart/form-data">' | |
+ 'File: <input type="file" name="fileupload" />' | |
+ '<input type="submit" value="Upload" />' | |
+ '</form><br><br>' | |
+ '<h1>Files</h1><ul>'; | |
for (let i = 0; i < result.resources.length; i += 1) { | |
html += '<li><a href="' | |
+ result.resources[i].secure_url + '">' | |
+ result.resources[i].public_id + '</a></li>'; | |
} | |
html += '</ul>'; | |
res.send(html); | |
}) | |
}) | |
app.post('/', upload.single('fileupload'), (req, res) => { | |
cloudinary.uploader.upload(req.file.path, (result) => { | |
res.send('<h1>Success</h1><a href="/">Home</a>'); | |
}) | |
}) | |
app.listen(PORT, () => console.log(`App listening on port ${PORT}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment