-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
181 lines (150 loc) · 5.05 KB
/
app.js
File metadata and controls
181 lines (150 loc) · 5.05 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require('dotenv').config();
const express = require("express");
const path = require("path");
const bodyParser = require('body-parser')
const multer = require('multer')
const fs = require("fs");
const bcrypt = require("bcryptjs")
const jwt = require("jsonwebtoken")
const cookieParser = require('cookie-parser')
const dialog = require("dialog");
require('./db/mongo')
const User = require('./db/user');
const Register = require('./db/register');
const auth = require("./middleware/auth");
const { err } = require('dialog');
const { db } = require('./db/user');
const app = express();
const port = 8000;
//EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static'))//for serving static files
app.use(cookieParser());
app.use(express.urlencoded());
app.use(bodyParser.urlencoded({extented:true}))
var storage = multer.diskStorage({
destination:function(req,file,cb){
cb(null,'uploads')
},
filename:function(req,file,cb){
cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
})
var upload = multer({
storage:storage
})
//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', auth, (req, res)=>{
const params = {};
res.status(200).render('notes.pug', params);
})
app.get('/addnotes', auth, (req, res)=>{
const params = {};
res.status(200).render('addnotes.pug', params);
})
app.get('/login',(req, res)=>{
const params = {};
res.status(200).render('login.pug', params);
})
app.get('/logout',auth,async (req, res)=>{
try {
res.clearCookie("jwt");
// console.log("Logout successfully")
// await req.user.save();
console.log("Logout successfully")
res.status(200).render('login.pug');
dialog.info("Logout successfully!!")
} catch (error) {
res.status(500).send(error);
}
})
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.send(`<script> alert("Sent") </script>`)
res.status(200).render('index.pug');
dialog.info("Message sent !!");
}) .catch(()=>{
// res.status(400).send("Item was not saved to the database .")
// res.send(`<script> window.alert("Unable to sent") </script>`)
dialog.info("Unable to sent !!");
})
})
app.post('/register',async (req, res) =>{
var registerData = new Register(req.body);
const token = await registerData.generateAuthToken();
//Saving the tokens in cookies..
res.cookie("jwt",token, {
expires:new Date(Date.now() + 600000),
httpOnly: true
});
registerData.save().then(() =>{
res.status(200).render('login.pug',dialog.info("Registered Successfully !!"));
}) .catch(()=>{
// res.status(400).send("Item was not saved to the database .")
dialog.info("Unable to register!!!")
})
})
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 = await bcrypt.compare(password, useremail.password);
const token = await useremail.generateAuthToken();
res.cookie("jwt",token, {
expires:new Date(Date.now() + 600000),
httpOnly: true,
});
if(isMatch){
res.status(200).render('notes.pug');
dialog.info("Successfully log in !!")
}else{
dialog.info("Invalid password !!")
}
} catch (error) {
// res.status(400).send("Invalid login details");
dialog.info("Invalid details !!")
}
})
app.post('/addnotes',upload.single('myfile'),(req,res,next) =>{
const file = req.file;
if(!file) {
const error = new Error("Please upload a file");
error.httpStatusCode = 400;
return next (error)
}
res.status(200).render('addnotes.pug');
dialog.info("File uploaded")
})
// app.post('/addnotes',upload.single('myfile'),(req,res) =>{
// var file = fs.readFileSync(req.file.path);
// var encode_file = file.toString('base64');
// // Defining a json object for the file
// var finalFile = {
// contentType:req.file.mimetype,
// path:req.file.path,
// file:new Buffer(encode_file,'base64')
// };
// //insert the file to the database
// db.collection('MyFile').insertOne(finalFile,(err,result) =>{
// console.log(result)
// if(err) return console.log(err)
// console.log("Saved to Database")
// })
// })
//START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
})