-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (104 loc) · 2.59 KB
/
app.js
File metadata and controls
128 lines (104 loc) · 2.59 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
if(process.env.NODE_ENV !== 'production'){
require('dotenv/config');
}
//required packages
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const Note = require("./models/Notes");
const mongoSanitize = require('express-mongo-sanitize');
const https = require('https');
const path = require('path');
const fs = require('fs');
const PORT = process.env.PORT;
const cors = require('cors');
const { count } = require('console');
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(mongoSanitize());
app.use(
mongoSanitize({
replaceWith: '_',
}),
);
app.use(cors({
origin: '*'
}));
//mongodb connection
mongoose.connect(process.env.db_connection, {});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", () => {
console.log("Database connected");
});
// routes
app.get("/", async (req, res) => {
try {
const notes = await Note.find({}, { passphrase: 0 });
res.json(notes);
} catch (e) {
console.log(e);
res.send("error");
}
});
app.post("/", async (req, res) => {
try {
const note = new Note(req.body);
await note.save();
console.log(note);
res.json(note);
} catch (e) {
console.log(e);
res.statusCode = 500;
res.send(e.message);
}
});
app.delete("/",async(req,res)=>{
try{
const {passphrase} = req.body;
const sentNote = await Note.findById(req.body._id);
if(sentNote.passphrase === passphrase){
await Note.remove(sentNote);
res.send('Deleted Successfully')
}else{
res.send('Incorrect passphrase');
}
}catch(e){
console.log(e,'Error in deleting note');
}
})
// app.delete("/deleteall/:pass", async(req,res)=>{
// try{
// if(req.params.pass === "dsc"){
// await Note.deleteMany({});
// res.send('all records deleted');
// }else{
// res.send('wrong pass');
// }
// }catch(e){
// console.log(e);
// res.send('error in deleting all records');
// }
// })
app.get("*", (req, res)=>{
res.send("Page doesn't exist");
});
if(process.env.NODE_ENV !== 'production'){
app.listen(PORT || 3000, () => {
console.log(`Serving on port ${PORT}`);
});
} else{
const sslServer = https.createServer(
{
key: fs.readFileSync(path.join(__dirname, 'certs', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'certs', 'cert.pem')),
},
app
)
sslServer.listen(PORT, () => console.log(`Secure Server running on port ${PORT}`));
}