· 2 years ago · Apr 30, 2023, 09:01 PM
1import express from 'express'
2import path from "path";
3import {authMiddleware} from "./middlewares/middlewares.js";
4import cookieParser from "cookie-parser";
5import jwt from "jsonwebtoken";
6import bcrypt from "bcryptjs";
7import { create, getAll, remove, update, getUpdateModel } from "./controllers/articleController.js";
8import { register, getByEmail } from "./controllers/userController.js";
9import {secretKey} from "./config.js";
10
11const __dirname = path.resolve()
12const PORT = 3000
13const app = express()
14let val = []
15
16app.use(express.json({limit: '1mb'}))
17app.use(express.urlencoded({extended: false}))
18app.set('view engine', 'ejs')
19app.set('views',path.resolve(__dirname, 'templates'))
20app.use(express.static('res'));
21app.use(cookieParser())
22
23function getData(data) {
24 val = data
25}
26
27app.get('/getArticlesByName/:name?', async (req, res)=>{
28 let articles = []
29 await getAll(getData)
30 articles = val
31 if (req.params.name === 'undefined') {
32 return res.status(200).send(articles)
33 } else {
34 return res.status(200).send(articles.filter(x => x.name.includes(req.params.name)))
35 }
36})
37
38app.get('/', authMiddleware, async (req, res)=>{
39 res.render('index', {authorId: req.cookies.current_user_id})
40})
41
42app.get('/dfy', authMiddleware, async (req, res)=>{
43 res.render('dfy', {authorId: req.cookies.current_user_id})
44})
45
46app.get('/edit/:id', authMiddleware, async (req, res)=>{
47 let article = []
48 await getUpdateModel(req.params.id, getData)
49 article = val
50 if (typeof article === 'undefined') return res.status(500).send("Article not found!");
51 if (req.cookies.current_user_id != article.author_id) return res.status(500).send("This article is not your!");
52 res.render('edit', {article: article})
53})
54
55app.get('/login', async (req, res)=>{
56 res.render('login')
57})
58
59app.get('/register', async (req, res)=>{
60 res.render('register')
61})
62
63app.post('/add', authMiddleware, async (req, res) => {
64 await create(req.body);
65 res.redirect('/');
66})
67
68app.post('/delete/:id', authMiddleware, async (req, res) => {
69 console.log(req.params.id)
70 await remove(req.params.id)
71 return res.redirect("/");
72})
73
74app.post('/edit', authMiddleware, async (req, res) => {
75 if (req.cookies.current_user_id != req.body.author_id) return res.status(500).send("This article is not your!");
76 await update(req.body)
77 return res.redirect("/");
78})
79
80app.post('/login', async(req, res) => {
81 try {
82 const {email, password} = req.body
83 await getByEmail(email, getData)
84 if (val === []) return res.status(500).send("Incorrect email!");
85 const isValidPassword = await bcrypt.compare(password, val[0].hash)
86 if (!(isValidPassword)) return res.status(500).send("Incorrect password!");
87 const token = jwt.sign({email, password}, secretKey, {expiresIn: "1h"})
88 res.cookie("token", token, {httpOnly:true})
89 res.cookie("current_user_id", val[0].id, {httpOnly:true})
90 return res.redirect("/");
91 }
92 catch {
93 return res.status(500).send("Server error!");
94 }
95
96})
97
98app.post('/exit', async(req, res) => {
99 res.clearCookie("token")
100 res.clearCookie("current_user_id")
101 return res.redirect("/login")
102})
103
104app.post('/register', async(req, res) => {
105 try {
106 const {email, password} = req.body
107 await getByEmail(email, getData)
108 if (val === []) return res.status(500).send("Email already register");
109 const hash = await bcrypt.hash(password, 10);
110 await register(email, hash);
111 return res.redirect("/");
112 }
113 catch {
114 return res.status(500).send("Server error!");
115 }
116})
117
118app.listen(PORT, ()=>{
119 console.log("workin")
120})