-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (80 loc) · 2.09 KB
/
index.js
File metadata and controls
91 lines (80 loc) · 2.09 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import express from "express";
import fs from "fs";
import mongoose from "mongoose";
import multer from "multer";
import cors from "cors";
import {
registerValidation,
loginValidation,
postCreateValidation,
} from "./validations.js";
import { UserController, PostController, CommentController } from "./controllers/index.js";
import { handleValidationErrors, checkAuth } from "./utils/index.js";
const app = express();
const storage = multer.diskStorage({
destination: (_, __, cb) => {
if(!fs.existsSync('uploads')) {
fs.mkdirSync('uploads');
}
cb(null, "uploads");
},
filename: (_, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({ storage });
app.use(cors());
mongoose
.connect(
process.env.MONGODB_URI
)
.then(() => console.log("DB is connected successfully"))
.catch((err) => console.log("DB error", err));
app.use(express.json());
app.use("/uploads", express.static("uploads"));
app.post(
"/auth/login",
loginValidation,
handleValidationErrors,
UserController.login
);
app.post(
"/auth/register",
registerValidation,
handleValidationErrors,
UserController.register
);
app.get("/auth/me", checkAuth, UserController.getMe);
app.post("/upload", checkAuth, upload.single("image"), (req, res) => {
res.json({
url: `/uploads/${req.file.originalname}`,
});
});
app.post("/comments/:id", checkAuth, CommentController.create)
app.get("/comments", CommentController.getLastComments)
app.get("/comments/:id", CommentController.get)
app.get("/posts", PostController.getAll);
app.get("/posts/popular", PostController.getPopular);
app.get("/tags", PostController.getLastTags);
app.get("/posts/:id", PostController.getOne);
app.post(
"/posts",
checkAuth,
postCreateValidation,
handleValidationErrors,
PostController.create
);
app.delete("/posts/:id", checkAuth, PostController.remove);
app.patch(
"/posts/:id",
checkAuth,
postCreateValidation,
handleValidationErrors,
PostController.update
);
app.listen(process.env.PORT || 4444 , (err) => {
if (err) {
return console.log(err);
}
console.log("Server OK");
});