-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
132 lines (109 loc) · 3.4 KB
/
server.js
File metadata and controls
132 lines (109 loc) · 3.4 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
const express = require('express');
const signUp = require('./controller/signUp');
const joi = require('joi')
const bcrypt = require('bcrypt-nodejs');
const { restart } = require('nodemon');
const cors = require('cors');
const parse = require("pg-connection-string").parse;
const knexx = require('knex');
// Parse the environment variable into an object containing User, Password, Host, Port etc at separate key-value pairs
const pgconfig = parse(process.env.DATABASE_URL);
// Add SSL setting to default environment variable on a new key-value pair (the value itself is an object)
pgconfig.ssl = { rejectUnauthorized: false };
const knex = knexx({
client: "pg",
connection: pgconfig,
});
const server = new express();
server.use(express.json());
server.use(cors());
var validUsers;
var loginInfo;
knex.select('*').from('users').then((resp)=>{
validUsers=resp;
// console.log(validUsers)
}).catch(err=>{console.log(err)});
knex.select('*').from('login').then((resp)=>{
loginInfo=resp;
// console.log(loginInfo)
}).catch(err=>{console.log(err)});
server.get('/', (req, res) => {
res.send("everything works");
})
server.get('/userList', (req, res) => {
knex.select('*').from('users').then((resp)=>{
validUsers=resp;
console.log(validUsers)
res.json(validUsers);
}).catch(err => {res.status(404).json("something went wrong: " + err)});
})
server.post('/user', (req, res) => {
knex
.select('*')
.from('users')
.where({
email:req.body.email
})
.then(data=>{
if(data.length>0){
console.log(data);
res.json(data[0]);
} else {
res.status(404).json("not found");
}
}).catch(err => {res.status(404).json("something went wrong")})
})
server.post('/signup', ((req, resp)=> signUp.handleRegister(req, resp, knex, bcrypt)))
server.post('/signin', (req, res) => {
knex('users')
.join('login', 'users.email', '=', 'users.email')
.select('*')
.then(data=>{
const user = data.find(u=> (u.email==req.body.email));
console.log(JSON.stringify(user), req.body);
bcrypt.compare(req.body.password, user.hash, function (err, resp) {
if(resp) {
res.json(user);
} else {
res.status(400).json("wrong password");
}
});
}).catch(err => {res.status(404).json("something went wrong")})
})
server.put('/postImage', (req, res) => {
knex('users')
.returning('*')
.where('id','=', req.body.id)
.increment('entries',1)
.then(response=>{
if(response.length>0){
res.send(response[0]);
} else {
res.status(404).send("not found");
}
})
.catch(err=>res.status(400).json("bad request"));
})
server.listen(process.env.PORT || 3000, () => {
console.log("Listening to the specified port")
})
findUserById = (id) => {
return validUsers.find(u => u.id === id)
}
findUserByEmail = (email) => {
return validUsers.find(u => u.email === email)
}
userInfoValidator = (body) => {
var schema = joi.object({
email: joi.string().email().required(),
password: joi.string().min(3).required(),
name: joi.string()
})
const result = schema.validate(body);
if (result.error) return result.error.details[0].message;
return false;
}
// login
// sign up
// profile
// image -> put