-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (193 loc) · 5.44 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// dotenv is for encryption of sensitive data
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
const mysql = require('mysql');
const router = require('./middleware/meta.routes');
const port = 5005;
const bcrypt = require('bcrypt');
const fs = require('fs');
const userDb = './db/users.json';
const jwt = require('jsonwebtoken');
const auth = require('./middleware/auth');
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: process.env.DATABASE_PASS,
database: 'ndina',
});
// Connect
db.connect((err) => {
if (err) {
// throw err;
// console.log(err.message);
}
console.log('MySql Connected');
});
// Create DB
app.get('/createdb', (request, response) => {
let sql = 'CREATE DATABASE ndina';
db.query(sql, (err, result) => {
if (err) {
throw err;
}
console.log(result);
response.send('Database created...');
});
});
// Create Table
app.get('/createpoststable', (request, response) => {
let sql =
'CREATE TABLE posts(id int AUTO_INCREMENT, title VARCHAR(255), body VARCHAR(255), PRIMARY KEY(id))';
db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
response.send('Posts table created...');
});
});
app.get('/addpost3', (request, response) => {
let post = { title: 'Post One', body: 'this is post number one' };
let sql = 'INSERT INTO posts SET ?';
let query = db.query(sql, post, (err, result) => {
if (err) throw err;
console.log(result);
response.send('Post added...');
});
});
// Select Posts
app.get('/getposts', (request, response) => {
let sql = 'SELECT * FROM posts';
let query = db.query(sql, (err, results) => {
if (err) throw err;
console.log(results);
response.send('Posts Fetched...');
});
});
// Select Single Post
app.get('/getpost/:id', (request, response) => {
let sql = `SELECT * FROM posts WHERE ${request.params.id}`;
let query = db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
response.send('Post Fetched...');
});
});
// Update Post
app.get('/updatepost/:id', (request, response) => {
let newTitle = 'Updated Title';
let sql = `UPDATE posts SET title='${newTitle}' WHERE ${request.params.id}`;
let query = db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
response.send('Post Updated...');
});
});
// Update Post
app.get('/deletepost/:id', (request, response) => {
let sql = `DELETE FROM posts WHERE ${request.params.id}`;
let query = db.query(sql, (err, result) => {
if (err) throw err;
console.log(result);
response.send('Post Deleted...');
});
});
app.use(cors());
app.use(express.json());
app.use('/', router);
app.listen(port, () => {
console.log('Server is listening on port ', port);
});
//@route get /loadUser
//@desc get token for user
//@access private
app.get('/loadUser', auth, async (request, response) => {
const { email } = request.user;
const initdb = await fs.readFileSync(userDb);
const user = await JSON.parse(initdb);
try {
const userData = await user.find((userEmail) => userEmail.email == email);
delete userData.password;
response.json(userData);
} catch (error) {
console.error(error.message);
response.status(500).send('Server Error');
}
});
//@route post /signUp
//@desc sign up user
//@access public
app.post('/signUp', async (request, response) => {
const { name, email, password } = request.body;
const initdb = await fs.readFileSync(userDb);
const user = await JSON.parse(initdb);
try {
if (user.some((userData) => userData.email == email)) {
return response.status(409).json({ msg: 'email already in use' });
} else {
const salt = await bcrypt.genSalt(2);
const hashedpassword = await bcrypt.hash(password, salt);
const newUserData = [
...user,
{ name: name, email: email, password: hashedpassword },
];
const data = JSON.stringify(newUserData, null, 2);
fs.writeFile(userDb, data, (err) => {
if (err) throw err;
console.log('Data written to file');
});
//payload can be set to user id once made
const payload = {
user: {
email: user.email,
},
};
jwt.sign(
payload,
process.env.JWT_SECRET,
{ expiresIn: 900 },
(err, token) => {
if (err) throw err;
response.json({ token });
}
);
}
} catch (error) {
console.error(error.message);
}
});
//@route post /login
//@desc log in user and get token
//@access public
app.post('/login', async (request, response) => {
try {
const { email, password } = request.body;
const initdb = await fs.readFileSync(userDb);
const parsed = await JSON.parse(initdb);
const user = await parsed.find((user) => user.email === email);
if (user == null) {
return response.status(400).json({ msg: 'Cannot find user' });
}
const match = await bcrypt.compare(password, user.password);
if (!match) {
return response.status(400).json({ msg: 'invalid credentials' });
}
//payload can be set to user id once made
const payload = {
user: {
email: user.email,
},
};
jwt.sign(
payload,
process.env.JWT_SECRET,
{ expiresIn: 900 },
(err, token) => {
if (err) throw err;
response.json({ token });
}
);
} catch (err) {
console.error(err.message);
}
});