-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
189 lines (149 loc) · 4.58 KB
/
app.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
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
182
183
184
185
186
187
188
189
// Import dependencies
const express = require("express");
const jwt = require("jsonwebtoken");
const bodyParser = require("body-parser");
const Joi = require("joi");
const fs = require("fs");
const crypto = require("crypto");
const bcrypt = require("bcrypt");
require("dotenv").config();
const app = express();
const port = 3000;
// Middleware for parsing JSON request bodies
app.use(bodyParser.json());
// Authentication middleware
const authenticateToken = (req, res, next) => {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];
if (token == null) {
return res.sendStatus(401);
}
jwt.verify(token, process.env.SECRET_KEY, (err, user) => {
if (err) {
return res.sendStatus(403);
}
req.user = user;
next();
});
};
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Internal Server Error");
});
// Load meetups data from meetups.json file
let meetups = [];
try {
const data = fs.readFileSync("meetups.json");
meetups = JSON.parse(data);
} catch (error) {
console.error("Error reading meetups.json:", error);
}
// Load users data from users.json file
let users = [];
try {
const data = fs.readFileSync("users.json");
users = JSON.parse(data);
} catch (error) {
console.error("Error reading users.json:", error);
}
// Routes
app.get("/meetups", (req, res) => {
// Remove private user information from meetups before sending the response
const sanitizedMeetups = meetups.map((meetup) => {
const { id, title, summary, address } = meetup;
return { id, title, summary, address };
});
res.json(sanitizedMeetups);
});
app.post("/meetups", authenticateToken, (req, res) => {
// Validate incoming data using Joi
const schema = Joi.object({
title: Joi.string().required(),
summary: Joi.string().required(),
address: Joi.string().required(),
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
const newMeetup = {
id: crypto.randomUUID(),
title: value.title,
summary: value.summary,
address: value.address,
};
meetups.push(newMeetup);
saveMeetups();
res.status(201).json(newMeetup);
});
app.patch("/meetups/:id", authenticateToken, (req, res) => {
const id = req.params.id;
const meetup = meetups.find((m) => m.id === id);
if (!meetup) {
return res.status(404).json({ error: "Meetup not found" });
}
// Validate incoming data using Joi
const schema = Joi.object({
title: Joi.string(),
summary: Joi.string(),
address: Joi.string(),
});
const { error, value } = schema.validate(req.body);
if (error) {
return res.status(400).json({ error: error.details[0].message });
}
meetup.title = value.title || meetup.title;
meetup.summary = value.summary || meetup.summary;
meetup.address = value.address || meetup.address;
saveMeetups();
res.json(meetup);
});
app.delete("/meetups/:id", authenticateToken, (req, res) => {
const id = req.params.id;
const meetupIndex = meetups.findIndex((m) => m.id === id);
if (meetupIndex === -1) {
return res.status(404).json({ error: "Meetup not found" });
}
const deletedMeetup = meetups.splice(meetupIndex, 1)[0];
saveMeetups();
res.json(deletedMeetup);
});
app.post("/signup", (req, res) => {
const { username, password } = req.body;
// Check if the username is already taken
if (users.find((user) => user.username === username)) {
return res.status(400).json({ error: "Username already taken" });
}
// Hash the password
const hashedPassword = bcrypt.hashSync(password, 10);
const newUser = {
id: crypto.randomUUID(),
username,
password: hashedPassword,
};
users.push(newUser);
saveUsers();
res.sendStatus(200);
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
const user = users.find((u) => u.username === username);
if (!user || !bcrypt.compareSync(password, user.password)) {
return res.status(401).json({ error: "Invalid credentials" });
}
const token = jwt.sign({ username: user.username }, process.env.SECRET_KEY);
res.json({ token });
});
// Function to save meetups data to meetups.json file
function saveMeetups() {
fs.writeFileSync("meetups.json", JSON.stringify(meetups, null, 2));
}
// Function to save users data to users.json file
function saveUsers() {
fs.writeFileSync("users.json", JSON.stringify(users, null, 2));
}
// Start the server
app.listen(port, () => {
console.log(`Server is listening on http://localhost:${port}`);
});