-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (59 loc) · 1.6 KB
/
server.js
File metadata and controls
77 lines (59 loc) · 1.6 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
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const Signin = require('./controllers/signin');
const Profile = require('./controllers/profile')
const Image = require('./controllers/image');
const db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : 'hagios',
database : 'smartbrain'
}
});
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.get('/', (req,res)=> {res.send(database.users);})
app.post('/signin',(req, res) => {Signin.handleSignin(req, res, db, bcrypt)})
app.post('/register', (req,res) => {
const { email, name, password} = req.body;
if (!email || !name || !password) {
return res.status(400).json('incorrect form submission');
}
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('unable to register'))
})
app.get('/profile/:id',(req, res) => { Profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { Image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { Image.handleApiCall(req, res,)})
app.listen(3000, ()=> {
console.log('app is running ');
});