-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 959 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import express from "express"
import multer from "multer"
import { fileUploadOnCloudinary } from "./utils/cloudinary";
import { upload } from "./middleware/multer.midleware";
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('File Uploading Using Multer and Cloudinary')
})
app.post("/upload",upload.fields([{ name: "avatar", maxCount: 1 }]), async (req,res) => {
try {
const avatarImage = req.files?.avatar[0]?.path;
if(!avatarImage)
{
return res.status(500).json("Error Occured While File Saving");
}
const avatar = await fileUploadOnCloudinary(avatarImage);
return res.status(200).json("File Uploaded Successfuly!");
} catch (error) {
console.log("Error Occured in /upload route", error);
res.status(500).json("Faild To Upload File")
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})