-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
101 lines (94 loc) · 2.75 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
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
100
101
const express = require('express');
const path = require('path');
const app = express();
const mongoose = require('mongoose');
// const bodyparser = require('body-parser')
const port = 80;
require("./db/connection");
// Defining Mongoose Schema
const contactSchema = new mongoose.Schema({
name: String,
phone: Number,
email: String,
password: String,
confirm_password: String,
});
// defining model
const Contact = mongoose.model('Contact', contactSchema);
// for serving static files
app.use('/static', express.static('static'))
app.use(express.urlencoded({extended:true}))
//set the template engine as HTML
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.set('views', path.join(__dirname,'views'))
// console.log(path.join(__dirname,'views'))
// ENDPOINTS
app.get('/', (req,res)=>{
const params = {}
res.status(200).render('index.html', params);
});
app.get("/login_page", (req, res)=>{
const params = {}
res.status(200).render('login_page.html', params);
});
app.get("/signup_page", (req, res)=>{
const params = {}
res.status(200).render('signup_page.html', params);
});
app.get("/team", (req, res)=>{
const params = {}
res.status(200).render('team.html', params);
});
// this function will be used when user will send post request
// app.post("/signup_page.html", (req, res)=>{
// console.log('post req');
// var myData = new Contact(req.body);
// myData.save().then(result=>{
// console.log(result);
// res.status(200).json({
// newContact:result
// })
// }).catch(err=>{
// console.log(err);
// res.status(500).json({
// error:err
// })
// });
// });
// app.post("/signup_page.html" , (req,res,next)=>{
// _id:new mongoose.Schema.Types.ObjectId,
// name = req.body.name,
// phone = req.body.phone,
// email = req.body.email,
// password = req.body.password,
// confirm_password = req.body.confirm_password,
// console.log(`${name} and phone is ${phone}`)
// });
app.post("/signup_page", (req, res)=>{
var myData = new Contact(req.body);
myData.save().then(()=>{
res.send("You've successfully registered on Client-Database. Go back to login/signIn.")
}).catch((err)=>{
res.status(400).send(err)
});
});
app.post("/login_page" , async(req,res)=>{
try {
const email = req.body.email;
const password = req.body.password;
// doubt after await
const useremail = await Contact.findOne({email:email});
// triple equaltoo
if(useremail.password === password){
res.status(201).render("team.html");
}else{
res.send("invalid input");
}
} catch(error) {
res.status(400).send("invalid email" + error)
}
})
app.listen(port, ()=>{
console.log(`The app started on port ${port}`);
});