forked from Mediun-org/Their-Verge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
199 lines (183 loc) · 4.83 KB
/
server.js
File metadata and controls
199 lines (183 loc) · 4.83 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
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
var express = require('express');
var request = require('request');
var path = require('path');
var http = require('http');
const app = express();
const port = process.env.PORT || 3000;
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const dotenv = require('dotenv');
const db = require('./data/db.js');
const Article = db.Article;
const Author = db.Author;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
dotenv.config();
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
var logedInUser = null;
//@Signup
app.post('/signup', (req, res) => {
const { name, email, password, imgUrl } = req.body;
if (!name || !email || !password || !imgUrl) {
return res.status(400).json({ msg: 'please enter all fileds!' });
}
Author.findOne({ email }).then(user => {
if (user) return res.status(400).json({ msg: 'user already exists!' });
const newUser = new Author({
id: Date.now(),
name,
email,
password,
imgUrl
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser.save().then(user => {
jwt.sign(
{ name: user.name },
process.env.jwtSecret,
{ expiresIn: 3600 },
(err, token) => {
if (err) throw err;
res.json({
token,
user: {
id: user.id,
name: user.name,
email: user.email
}
});
}
);
});
});
});
});
});
//@Login
app.post('/signin', (req, res) => {
console.log(req.body);
const { email, password } = req.body;
// Simple validation
if (!email || !password) {
return res.status(400).json({ msg: 'Please enter all fields' });
}
// Check for existing user
Author.findOne({ email }, function(err, user) {
if (!user) return res.status(400).json({ msg: 'User Does not exist' });
console.log(user);
// Validate password
bcrypt.compare(password, user.password).then(isMatch => {
if (!isMatch) return res.status(400).json({ msg: 'Invalid credentials' });
jwt.sign(
{ name: user.name },
process.env.jwtSecret,
{ expiresIn: 3600 },
(err, token) => {
if (err) throw err;
res.cookie('token', token);
res.redirect('back');
logedInUser = [user.id, user.name, user.imgUrl];
res.json({
token,
user: {
id: user.id,
name: user.name,
email: user.email
}
});
}
);
});
});
});
//@logout
app.get('/logout', (req, res) => {
res.clearCookie('token');
logedInUser = null;
res.redirect('back');
});
//----------post----------------
app.get('/article/:id', (req, res) => {
var id = req.params.id;
console.log('the id in post is: ', id);
var arr = [];
db.selectAll(
Article,
(err, art) => {
console.log('Hendd', art[0]);
arr.push(art[0]);
db.selectAll(
Author,
(err, author) => {
arr.push(author[0]);
db.selectAll(Article, (err, arts) => {
arr.push(arts);
console.log('this the arr in server: ', arr);
res.status(202).send(arr);
});
},
art[0].authorId
);
},
id
);
});
//---------------Comments----------------
app.get('/comments/:id', (req, res) => {
var id = req.params.id;
// console.log(id);
db.selectById(Article, id, function(err, data) {
if (err) {
console.log(err);
} else {
const comments = data[0].comments;
const ids = [];
comments.map(e => {
ids.push(e.userId);
});
Author.find({
id: {
$in: ids
}
}).then(result => {
// console.log(result);
for (let i = 0; i < result.length; ++i) {
comments[i]['name'] = result[i]['name'];
comments[i]['imgUrl'] = result[i]['imgUrl'];
}
res.json({ comments, logedInUser });
});
}
});
});
app.post('/sendComment/:id', (req, res) => {
var id = req.params.id;
const comment = req.body;
comment['userId'] = parseInt(comment['userId']);
Article.update({ id: id }, { $push: { comments: comment } }).exec(function(
err,
result
) {
if (err) {
console.log(err);
} else {
console.log(result);
res.json(comment);
}
});
});
//--------------------------------------
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(port, () => {
console.log(`server running at: http://localhost:${port}`);
});