Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (77 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
92 lines (77 loc) · 2.05 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
92
import cors from 'cors';
import express from 'express';
import fs from 'fs';
import multer from 'multer';
import mongoose from 'mongoose';
import {
loginValidation,
postCreateValidation,
registerValidation,
} from './validations.js';
import { checkAuth, handleValidationErrors } from './utils/index.js';
import { PostController, UserController } from './controllers/index.js';
mongoose
.connect(
'mongodb+srv://root:mayfield@cluster0.wugylgo.mongodb.net/blog?retryWrites=true&w=majority'
)
.then(() => console.log('DB ok'))
.catch(err => console.log('DB error', err));
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(express.json());
app.use(cors());
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.get('/tags', PostController.getLastTags);
app.get('/posts', PostController.getAll);
app.get('/posts/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');
});