-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (68 loc) · 2.14 KB
/
app.js
File metadata and controls
80 lines (68 loc) · 2.14 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
78
79
80
const express = require("express");
const path = require("path");
const fs = require("fs");
const bcrypt = require("bcryptjs")
require('./db/mongo')
const User = require('./db/user');
const Register = require('./db/register');
const app = express();
const port = 8000;
//EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static'))//for serving static files
app.use(express.urlencoded());
//PUG SPECIFIC STUFF
app.set('view engine', 'pug')//set the template engine as pug
app.set('views',path.join(__dirname, 'views'))//set the views directory
//ENDPOINTS
app.get('/',(req, res)=>{
const params = {};
res.status(200).render('index.pug', params);
})
app.get('/notes',(req, res)=>{
const params = {};
res.status(200).render('notes.pug', params);
})
app.get('/login',(req, res)=>{
const params = {};
res.status(200).render('login.pug', params);
})
app.get('/register',(req, res)=>{
const params = {};
res.status(200).render('register.pug', params);
})
app.post('/',(req, res) =>{
var myData = new User(req.body);
myData.save().then(() =>{
// res.send("This item has been saved to the database")
res.status(200).render('index.pug');
}) .catch(()=>{
res.status(400).send("Item was not saved to the database .")
})
})
app.post('/register',(req, res) =>{
var registerData = new Register(req.body);
registerData.save().then(() =>{
res.status(200).render('login.pug');
}) .catch(()=>{
res.status(400).send("Item was not saved to the database .")
})
})
app.post('/login',async (req, res)=>{
try {
const email = req.body.email;
const password = req.body.password;
const useremail = await Register.findOne({email:email});
const isMatch = bcrypt.compare(password, useremail.password);
if(isMatch){
res.status(200).render('notes.pug');
}else{
res.send("Invalid");
}
} catch (error) {
res.status(400).send("Invalid login details");
}
})
//START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
})