-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (56 loc) · 2.03 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
import express from "express";
import mongoose from "mongoose";
import multer from "multer";
import { postCreateValidation, registerValidation } from './validations/auth.js';
import dotenv from 'dotenv'
import checkAuth from "./utils/checkAuth.js";
import { createPost, getOne, getPosts, remove, update } from "./controllers/postController.js";
import { login, register, userInfo } from "./controllers/userController.js"
import { addPortal } from "./controllers/portalController.js";
// dotenv
dotenv.config()
// use in express plugins
let app = express();
app.use(express.json())
app.use('/uploads', express.static('uploads'))
const port = process.env.PORT;
// image upload
const storage = multer.diskStorage({
destination: (_, __, cb) => {
cb(null, 'uploads')
},
filename: (_, file, cb) => {
cb(null, file.originalname)
}
})
const uploads = multer({ storage })
// mongoose configuration
mongoose.set('strictQuery', false);
mongoose.connect(process.env.HOST_TOKEN).then(() => {console.log('mongoose connected')}).catch((err) => console.log(err.mongoose))
// test *
app.get('/hello', (req, res, next) => {
res.json(["Tony", "Lisa", "Michael", "Ginger", "Food"]);
})
// login ****************************************
app.post('/auth/login', login)
// register ***************************************
app.post('/auth/register', registerValidation, register)
// usr info ************************************
app.get('/auth/me', checkAuth, userInfo)
// post ********************************
app.post('/portal', addPortal)
app.get('/posts', getPosts)
app.get('/posts/:id', getOne)
app.post('/posts', postCreateValidation, createPost)
app.delete('/posts/:id', checkAuth, remove)
app.patch('/update/:id', checkAuth, update)
// save img **************************
app.post('/upload', checkAuth, uploads.single('image'), (req, res) => {
res.json({ url: `upload/${req.file.originalname}` })
})
app.listen(process.env.PORT, (err) => {
if (err) {
return console.log(err)
}
console.log(`Server listening on port:${port}`)
})