-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (88 loc) · 2.39 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
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
require("dotenv").config();
const { request } = require("express");
const express = require("express");
const path = require("path");
const {
findInDataBase,
replaceOne,
listDbEntries,
addEntryToDB,
deleteOne,
} = require("./utils/database");
const { loadingAnimation } = require("./utils/load");
const app = express();
app.use(express.json());
const port = process.env.PORT || 3002;
//! List all entries
app.get("/api/passwords", async (request, response) => {
console.log("test");
const allEntries = await listDbEntries(false);
console.log({ allEntries });
response.send(allEntries);
});
app.delete("/api/passwords/:title", function (req, res) {
const { title } = req.params;
try {
deleteOne(title);
res.send(`${title} was deleted! say goodby`);
} catch (error) {
console.error(error);
response
.status(500)
.send(
"An unexpected error occured. Please reboot your Computer or Press ALT+F4"
);
s;
}
});
//! List a specific name
app.get("/api/passwords/:name", async (request, response) => {
try {
const { name } = request.params;
const passwordValue = await findInDataBase(name);
// passwordValue.json();
console.log("pwValue", passwordValue);
if (!passwordValue) {
response.status(404).send("No password for input");
return;
}
response.json(passwordValue);
} catch (error) {
console.error(error);
response.status(419).send("Internal Server Error because of TeaPot");
}
});
//! Add a entry
app.post("/api/passwords", async (request, response) => {
const newEntry = request.body;
try {
await addEntryToDB((terminal = false), {
title: newEntry.title,
email: newEntry.email,
pwd: newEntry.pwd,
});
response.send(`Yay, password for ${newEntry.title} set! `);
} catch (error) {
console.error(error);
response
.status(500)
.send(
"An unexpected error occured. Please reboot your Computer or Press ALT+F4"
);
}
});
app.use(express.static(path.join(__dirname, "client/build")));
app.use(
"/storybook",
express.static(path.join(__dirname, "client/storybook-static"))
);
app.get("*", (request, response) => {
response.sendFile(path.join(__dirname, "client/build", "index.html"));
});
async function run() {
await loadingAnimation();
app.listen(port, () => {
console.log(`API listening at http://localhost:${port}`);
});
}
run();