-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
189 lines (161 loc) · 4.59 KB
/
express.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// imports
import express from "express";
import bodyParser from "body-parser";
import pkg from 'pg';
import session from 'express-session';
import flash from 'connect-flash';
// constants
const { Pool } = pkg;
const app = express();
const port = 8080
const current_date = new Date();
// get the session and flash
app.use(session(
{
secret: 'cs312', resave: false, saveUninitialized: true
}));
app.use(flash());
app.use((req, res, next) =>
{
res.locals.messages = req.flash();
next();
});
// get the database information
const pool = new Pool
({
user: 'postgres', host: 'localhost', database: 'BlogDB',
password: '333964-Rf', port: 5432,
});
// setting up the ejs and url
app.set('view engine', 'ejs');
app.use(express.static('static'));
app.use(bodyParser.urlencoded({ extended: true }));
// access html script
app.get('/', async(req, res) =>
{
// get the userid
const userid = req.session.userid;
// check if user is logged in
if (!userid)
{
req.flash('error', 'Please log in to view your posts.');
return res.redirect('/login');
}
// get the blog posts for specific user
const result = await pool.query
('SELECT * FROM blogs WHERE userid = $1', [userid]);
const blogPosts = result.rows;
res.render('index.ejs', { blogPosts });
});
app.post('/create-form', async (req, res) =>
{
// get the information from user
const { title, content, name } = req.body;
const creationTime = current_date;
const userid = req.session.userid;
// have user log in if not already
if (!userid)
{
req.flash('error', 'Please log in to create a blog post.');
return res.redirect('/login');
}
// insert information into the database
await pool.query
(
'INSERT INTO blogs (creator_name, title, body, date_created, userid) VALUES ($1, $2, $3, $4, $5)',
[name, title, content, creationTime, userid]
);
// go back to homepage
res.redirect('/');
});
app.get('/edit/:id', async (req, res) => {
const postId = req.params.id;
const userid = req.session.userid;
const result = await pool.query
(
'SELECT * FROM blogs WHERE blog_id = $1 AND userid = $2',
[postId, userid]
);
const post = result.rows[0];
res.render('edit.ejs', { post, postId });
});
// handle the edit
app.post('/edit/:id', async (req, res) =>
{
// get the information from the user and blog post
const postId = req.params.id;
const { title, content, name } = req.body;
const userid = req.session.userid;
// add information to database
await pool.query
(
'UPDATE blogs SET title = $1, body = $2, creator_name = $3 WHERE blog_id = $4 AND userid = $5',
[title, content, name, postId, userid]
);
//go back to homepage
res.redirect('/');
});
// deal with deletes
app.post('/delete/:id', async (req, res) =>
{
// get the exact post number
const postId = req.params.id;
const userid = req.session.userid;
// Delete the post from the database
await pool.query
(
'DELETE FROM blogs WHERE blog_id = $1 AND userid = $2',
[postId, userid]
);
// go back to homepage
res.redirect('/');
});
// logging in
app.get('/login', (req, res) =>
{
res.render('login.ejs');
});
app.post('/login', async (req, res) =>
{
// get the username and password from user
const { username, password } = req.body;
// get the user from the database
const result = await pool.query
(
'SELECT * FROM users WHERE name = $1 AND password = $2',
[username, password]
);
// make sure username and password exists
if (result.rows.length > 0)
{
// if it does then we log it into the session
req.session.userid = result.rows[0].userid;
// go to homepage
res.redirect('/');
}
else
{
// Makes user try again if not valid
req.flash('error', 'Invalid Username or Password. Please try again.');
res.redirect('/login');
}
});
// creating user
app.get('/create-profile', (req, res) =>
{
res.render('create-profile.ejs');
});
app.post('/create-profile', async (req, res) =>
{
// get the name and password from html page
const { username, password } = req.body;
// add users name and password to the database
await pool.query('INSERT INTO users (name, password) VALUES ($1, $2)', [username, password]);
// go back to homepage
res.redirect('/login');
});
// start server at the port
app.listen(port, () =>
{
console.log(`Server is running on port ${port}.`);
});