-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
153 lines (139 loc) · 3.35 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
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
const express = require('express');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const Clarifai = require('clarifai');
const clarifai = new Clarifai.App({
apiKey: '90945e092129421ba4a256a756a7781c'
});
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const db = knex({
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: true,
}
});
const app = express ();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
const database = {
users: [
{
id: '123',
name: 'John',
email: '[email protected]',
password: 'cookies',
entries: 0,
joined: new Date()
},
{
id: '124',
name: 'Sally',
email: '[email protected]',
password: 'bananas',
entries: 0,
joined: new Date()
}
],
login: [
{
id: '987',
hash: '',
email: '[email protected]'
}
]
}
app.get('/', (req, res)=>{
res.send('it is working');
})
app.get('/profile/:id', (req, res) => {
const { id } = req.params;
db.select('*').from('users').where({
id: 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'))
// if (!found) {
// res.status(400).json('not found');
// }
})
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 get entries'))
})
app.post('/signin', (req, res)=>{
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json('invalid data');
}
db.select('email', 'hash').from('login')
.where('email', '=', email)
.then(data=> {
const isValid = bcrypt.compareSync(password, data[0].hash);
if (isValid){
return db.select('*').from('users')
.where('email', '=', 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;
if (!email || !name || !password) {
return res.status(400).json('invalid data');
}
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({
email: loginEmail[0],
name: name,
joined: new Date()
})
.then(user => {
res.json(user[0])
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => res.status(400).json(err))
})
app.listen(process.env.PORT || 3000, () => {
console.log(`app running on port ${process.env.PORT}`);
})
app.post('/imageurl', (req, res) => {
clarifai.models.predict(Clarifai.FACE_DETECT_MODEL, req.body.input)
.then(data => {
res.json(data);
})
.catch(err => res.status(400).json('unable api'))
})