-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (67 loc) · 1.84 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const knex = require('knex');
const bcrypt =require('bcrypt-nodejs');
const register= require('./controllers/register');
const db= knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '',
database : 'smartbrain'
}
});
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req,res)=> {
res.send(database.users);
})
app.post('/signin', (req, res) => {
const {email, password, name}= req.body;
if(!email || !password){
return res.status(400).json('Insert email and password')
}
db.select('email', 'hash').from('login')
.where('email', '=', req.body.email)
.then(data=> {
const isValid = bcrypt.compareSync(req.body.password, data[0].hash);
if(isValid){
return db.select('*').from('users')
.where('email', '=', req.body.email)
.then(users => {
res.json(users[0])
})
.catch(err => res.status(400).json('unable to get user'))
}
else{
res.status(400).json('wrong credentials')
}
})
.catch(err => res.status(400).jaon('wrong credentials'))
})
app.post('/register', (req,res) => {register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => {
const{ id }= req.params;
db.select('*').from('users').where({
id:id
})
.then(user =>{
res.json(user[0]);
})
})
app.put('/image',(req,res) => {
const{ id} = req.body;
db('users').where('id', '=', id)
.increment('entries',1)
.returning('entries')
.then(entries =>{
res.json(entries[0]);
})
.catch(err => res.status(400).json('unable to find legth'))
})
app.listen(3000, () =>{
console.log('app running on port 3000!')
})