-
Notifications
You must be signed in to change notification settings - Fork 281
/
server.js
109 lines (100 loc) · 3.14 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
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
const express = require('express');
const bodyParser = require('body-parser'); // latest version of exressJS now comes with Body-Parser!
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex')
const db = knex({
// Enter your own database information here based on what you created
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'aneagoie',
password : '',
database : 'smart-brain'
}
});
const app = express();
app.use(cors())
app.use(express.json()); // latest version of exressJS now comes with Body-Parser!
// Test only - when you have a database variable you want to use
// app.get('/', (req, res)=> {
// res.send(database.users);
// })
app.post('/signin', (req, res) => {
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(user => {
res.json(user[0])
})
.catch(err => res.status(400).json('unable to get user'))
} else {
res.status(400).json('wrong credentials')
}
})
.catch(err => res.status(400).json('wrong credentials'))
})
app.post('/register', (req, res) => {
const { email, name, password } = req.body;
const hash = bcrypt.hashSync(password);
db.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
// If you are using knex.js version 1.0.0 or higher this now returns an array of objects. Therefore, the code goes from:
// loginEmail[0] --> this used to return the email
// TO
// loginEmail[0].email --> this now returns the email
email: loginEmail[0].email,
name: name,
joined: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => res.status(400).json('unable to register'))
})
app.get('/profile/:id', (req, res) => {
const { id } = req.params;
db.select('*').from('users').where({id})
.then(user => {
if (user.length) {
res.json(user[0])
} else {
res.status(400).json('Not found')
}
})
.catch(err => res.status(400).json('error getting user'))
})
app.put('/image', (req, res) => {
const { id } = req.body;
db('users').where('id', '=', id)
.increment('entries', 1)
.returning('entries')
.then(entries => {
// If you are using knex.js version 1.0.0 or higher this now returns an array of objects. Therefore, the code goes from:
// entries[0] --> this used to return the entries
// TO
// entries[0].entries --> this now returns the entries
res.json(entries[0].entries);
})
.catch(err => res.status(400).json('unable to get entries'))
})
app.listen(3000, ()=> {
console.log('app is running on port 3000');
})