-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (51 loc) · 1.43 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
64
65
66
67
import express from "express";
import authRoutes from "./routes/auth.js";
import postRoutes from "./routes/posts.js";
import usersRoutes from "./routes/users.js";
import cookieParser from "cookie-parser";
import multer from "multer";
import cors from "cors";
import { v2 as cloudinary } from "cloudinary";
import { CloudinaryStorage } from "multer-storage-cloudinary";
import "./loadEnv.js";
const app = express();
// CORS
const frontURL = process.env.FRONT_URL;
app.use(
cors({
credentials: true,
origin: frontURL,
methods: ["GET", "POST", "PUT", "DELETE"],
optionsSuccessStatus: 200,
})
);
// USE JSON & COOKIE PARSER
app.use(express.json());
app.use(cookieParser());
// CLOUDINARY
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_KEY,
api_secret: process.env.CLOUDINARY_SECRET,
});
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: "blog",
allowed_formats: ["jpg", "png", "webp", "svg"],
public_id: (req, file) => "blog",
},
});
const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), function (req, res) {
res.json(req.file.filename);
});
// ROUTES /
app.use("/api/auth", authRoutes);
app.use("/api/posts", postRoutes);
app.use("/api/users", usersRoutes);
//PORT
const port = process.env.PORT || 8800;
app.listen(port, () => {
console.log(`Connected on http://localhost:${port}`);
});