-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
52 lines (38 loc) · 1.34 KB
/
events.js
File metadata and controls
52 lines (38 loc) · 1.34 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
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
let events = [{
id: "taking-the-leap-entrepreneurship",
title: "Taking the leap into Entrepreneurship: My Journey",
startDateTime: "2025-09-15T18:00:00-07:00",
endDateTime: "2025-09-15T19:00:00-07:00",
location: "Student Union Theatre"
}]
app.post('/create-events', (req, res) => {
const {title, startDateTime, endDateTime, location, createdBy} = req.body;
if (!title || !startDateTime|| !endDateTime || !location || !createdBy)
res.status(400).send({message: "Missing one or more properties"});
else{
//create id by lowercasing and replacing spaces with -
const id = title.toLowerCase().replace(/\s+/g, "-");
const newEvent = {id, title, startDateTime, endDateTime, location};
events.push(newEvent);
res.status(201).send(events);
}
})
app.get('/events', (req, res) => {
res.status(200).send(events);
})
app.get('/events/:id', (req, res) => {
const id = req.params.id;
const event = events.find(e => e.id === id)
if (event) {
res.status(200).send(event);
} else {
res.status(200).send(event);
}
})
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
})