-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (92 loc) · 2.83 KB
/
server.js
File metadata and controls
99 lines (92 loc) · 2.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
const express = require('express')
const app = express();
const bodyParser = require('body-parser')
const cors = require('cors')
const bcrypt = require('bcrypt');
const Clarifai = require('clarifai')
app.use(cors())
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
const knex = require('knex')({
client: 'pg',
connection: {
host: '127.0.0.1',
user: 'postgres',
password: 'dat',
database: 'postgres'
}
});
app.post('/signin', (req, res) => {
const {password,email} = req.body
if (!email || !password){
return res.status(400).json('Incorrect form submit')
}
knex
.select('email', 'hash')
.from('login')
.where('email', '=', req.body.email)
.then(data => {
const passwordValie = bcrypt.compareSync(req.body.password, data[0].hash)
if (passwordValie) {
knex
.select('*')
.from('users')
.where('email', '=', req.body.email)
.then(user => res.json(user[0]))
.catch(err => res.status(400).json('unable to get user'))
}
})
.catch(err => res.status(400).json('Wrong password'))
})
app.post('/register', (req, res) => {
const {
name,
email,
password
} = req.body
if (!email || !password || !name){
return res.status(400).json('Incorrect form submit')
}
const hash = bcrypt.hashSync(password, 1);
knex.transaction(trx => {
trx('login')
.insert({
email: email,
hash: hash
})
.returning('email')
.then(emailRegis => {
return trx('users').insert({
name: name,
email: emailRegis[0],
datejoined: new Date()
})
.returning('*')
.then(user => res.json(user[0]))
})
.then(trx.commit)
.catch(trx.rollback);
}).catch(err => res.status(400).json('get error'))
})
app.put('/image', (req, res) => {
knex('users')
.where('name', '=', req.body.name)
.increment('entries', 1)
.returning('entries')
.then(entries => {
res.json(entries[0])
})
.catch(err => res.status(400).json('Cant get the user'))
})
app.post('/imageurl', (req,res)=>{
const app = new Clarifai.App({apiKey: '283d55b4e41b4a85accac4fbe342ce38'});
app.models.initModel({id: Clarifai.FACE_DETECT_MODEL})
.then(generalModel => generalModel.predict(req.body.input))
.then(data => res.json(data))
.catch(err => res.status(400).json('err'))
})
app.listen(3000, () => {
console.log('App is running on 3000')
})