-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
78 lines (66 loc) · 1.74 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const knex = require('knex');
const db = knex({
client: 'pg',
connection: {
host: '127.0.0.1',
user: 'postgres',
password: 'test',
database: 'loginformytvideo'
}
})
const app = express();
let intialPath = path.join(__dirname, "public");
app.use(bodyParser.json());
app.use(express.static(intialPath));
app.get('/', (req, res) => {
res.sendFile(path.join(intialPath, "index.html"));
})
app.get('/login', (req, res) => {
res.sendFile(path.join(intialPath, "login.html"));
})
app.get('/register', (req, res) => {
res.sendFile(path.join(intialPath, "register.html"));
})
app.post('/register-user', (req, res) => {
const { name, email, password } = req.body;
if(!name.length || !email.length || !password.length){
res.json('fill all the fields');
} else{
db("users").insert({
name: name,
email: email,
password: password
})
.returning(["name", "email"])
.then(data => {
res.json(data[0])
})
.catch(err => {
if(err.detail.includes('already exists')){
res.json('email already exists');
}
})
}
})
app.post('/login-user', (req, res) => {
const { email, password } = req.body;
db.select('name', 'email')
.from('users')
.where({
email: email,
password: password
})
.then(data => {
if(data.length){
res.json(data[0]);
} else{
res.json('email or password is incorrect');
}
})
})
app.listen(3000, (req, res) => {
console.log('listening on port 3000......')
})