-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
100 lines (85 loc) · 2.45 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
require("dotenv").config();
const express = require("express");
const path = require("path");
const {
getPassword,
setPassword,
getPasswords,
deletePassword,
} = require("./lib/passwords");
const { connect } = require("./lib/database");
const app = express();
app.use(express.json());
const port = process.env.PORT || 3600;
app.get("/api/passwords/:name", async (request, response) => {
const { name } = request.params;
try {
const passwordValue = await getPassword(name);
if (!passwordValue) {
response
.status(404)
.send("Could not find the password you have specified");
return;
}
response.send(passwordValue);
} catch (error) {
console.error(error);
response.status(500).send("An internal server error occured");
}
});
app.delete("/api/passwords/:name", async (request, response) => {
const { name } = request.params;
try {
const deleted = await deletePassword(name);
if (deleted.deletedCount === 0) {
response
.status(404)
.send("Could not find the password you have specified");
return;
}
response.json("Password deleted");
} catch (error) {
console.error(error);
response.status(500).send("An internal server error occured");
}
});
app.get("/api/passwords", async (request, response) => {
try {
const passwords = await getPasswords();
response.json(passwords);
} catch (error) {
console.error(error);
response
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
app.post("/api/passwords", async (request, response) => {
const password = request.body;
try {
await setPassword(password.name, password.value);
response.send(`Successfully set ${password.name} `);
} catch (error) {
console.error(error);
response
.status(500)
.send("An unexpected error occured. Please try again later!");
}
});
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() {
console.log("Connecting to database...");
await connect(process.env.MONGO_DB_URI, process.env.MONGO_DB_NAME);
console.log("Connected to database 🎉");
app.listen(port, () => {
console.log(`PW4U API listening at http://localhost:${port}`);
});
}
run();