-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
63 lines (52 loc) · 1.4 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const path = require("path");
const express = require("express");
const fileUpload = require("express-fileupload");
const cloudinary = require("./config");
// dotenv
require("dotenv").config();
const PORT = process.env.PORT || 4000;
const app = express();
app.set("view engine", "hbs");
app.set("views", path.join(__dirname, "views"));
// middleware
app.use("/", express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(
fileUpload({
useTempFiles: true,
limits: { fileSize: 50 * 1024 * 1024 },
})
);
app.get("/", (req, res) => {
res.render("index", {
title: "Uploading images to Cloudinary Console",
});
});
app.post("/upload", async (req, res) => {
if (!req.files || Object.keys(req.files).length === 0) {
res.status(400).json({
msg: "No files were uploaded. Try uploading an image",
});
return;
}
const uploadFile = req.files.uploadFile;
console.log(uploadFile);
const result = await cloudinary.uploader.upload(uploadFile.tempFilePath, {
public_id: uploadFile.name,
resource_type: "auto",
folder: "uploaded",
use_filename: true,
unique_filename: false,
});
if (result.url) {
res.render("media", {
img: result.url,
name: uploadFile.name.replace(/.jpeg|.jpg|.png|.webp/gi, ""),
});
} else {
res.render("/upload");
}
});
app.listen(PORT, () => {
console.log(`server listening on port ${PORT}`);
});