-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (52 loc) · 1.85 KB
/
app.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
require('dotenv').config()
const express = require("express");
const path = require('path');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const register = require("./controllers/register");
const signIn = require("./controllers/signin");
const profile = require("./controllers/profile");
const image = require("./controllers/image");
const port = process.env.PORT || 3001;
const DB_HOST = process.env.ztm_sbdb_host;
const DB_PORT = process.env.ztm_sbdb_port;
const DB_USER = process.env.ztm_sbdb_user;
const DB_PW = process.env.ztm_sbdb_pw;
const DB_NAME = process.env.ztm_sbdb;
const db = require('knex')({
client: 'pg',
connection: {
host : DB_HOST,
port : DB_PORT,
user: DB_USER,
password : DB_PW,
database : DB_NAME
}
//searchPath: ['knex', 'public'],
});
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(cors());
// Setting up the public directory
app.use(express.static('public'));
function getUser(id) {
let found = false;
db.users.forEach(user => {
if (user.id === id) {
found = true;
return res.status(200).json(user);
}
})
if (!found) {
res.status(400).json('not found');
}
}
app.get("/", (req, res) => res.status(200).sendFile(path.join(__dirname, '/public/index.html')));
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) }) // dependency injuection;
// app.post('/signin', (req, res) => { signIn.handleSignIn(req, res, db, bcrypt) })
app.post('/signin', signIn.handleSignIn(db, bcrypt) )
app.get('/profile/:id', (req, res) => { profile.handleProfile(req, res, db) })
app.put('/image', (req, res) => { image.handleIMG(req, res, db) })
app.post('/imageURL', (req, res) => { image.handleAPICall(req, res) })
app.listen(port, () => console.log(`App listening on port ${port}!`));