-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
75 lines (63 loc) · 2.37 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
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs'); // Using npm package bcrypt to encrypt password
const cors = require('cors');
const knex = require('knex') // Using npm package knex to connect server with database
const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');
const db = knex({ // db is an alias for database
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
}
});
/******* For localhost *******/
// const db = knex({ // db is an alias for database
// client: 'pg',
// connection: {
// host : '127.0.0.1',
// user : 'postgres', // Run \d command in postgress will return lists of relations (It also gives informationn about owner which here is postgres in user object)
// password : '',
// database : 'Smart Brain'
// }
// });
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.get('/', (req, res) => { res.send('working!') })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db) }) // Currently this controller is not in use with front-end application (but can be helpful in future to add-on extra functionality)
app.put('/image', (req, res) => { image.handleImage(req, res, db) })
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})
app.listen(process.env.PORT || 3001, () => {
console.log(`App is running on port ${process.env.PORT}`);
})
/*********** Overview ***********
/ --> res = this is working
/signin --> POST = success/fail
/register --> POST = user
/profile/:userId --> GET = user
/image --> PUT --> user
*********************************/
/********* Table Queries *********
> Users Table
CREATE TABLE users (
id serial PRIMARY KEY,
name VARCHAR(100),
email text UNIQUE NOT NULL,
entries BIGINT DEFAULT 0,
joined TIMESTAMP NOT NULL
);
> Login Table
CREATE TABLE login (
id serial PRIMARY KEY,
hash VARCHAR(100) NOT NULL,
email text UNIQUE NOT NULL
);
*********************************/