Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
10 changes: 10 additions & 0 deletions classwork/index-2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<h1 id="heading"></h1>
<script>
fetch("http://localhost:3000/people")
.then((response) => response.json())
.then((data) => (document.getElementById("heading").innerHTML = data));
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions classwork/server-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const express = require("express");

const app = express();
app.use(express.json());

let people = ["Jason"];

app.get("/people", (request, response) => {
response.setHeader("Access-Control-Allow-Origin", "*");
response.json(people);
});

app.put("/people", (request, response) => {
people.push(...request.body);
response.json(people);
});


app.delete("/people/:name", (request, response) => {
const nameToDelete = request.params.name;
const indexToDelete = people.findIndex((name) => name === nameToDelete);
if (indexToDelete === -1) {
response.status(404).send("Person not found");
} else {
people.splice(indexToDelete, 1);
response.send("Person deleted");
}
});

const port = 3000;
app.listen(port, () => {
console.log("listening on port " + port);
});
21 changes: 10 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
<title>Welcome to CYF Chat</title>
</head>
<body>
<h1>
CYF Chat
</h1>
<h2>
Send a message
</h2>
<form action="/messages" method="post">
<h1>CYF Chat</h1>
<h2>Send a message</h2>
<form action="http://localhost:9090/messages" method="post">
<p>
Name: <input type="text" name="from" placeholder="Your Name" /> <br />
Message: <input type="text" name="text" placeholder="The message..." />
<br />
</p>
<button type="submit">
Send
</button>
<button type="submit">Send</button>
</form>

<a href="/messages">See all messages</a>
<a href="http://localhost:9090/messages" method="GET">See all messages</a>
<!-- <script>
fetch("http://localhost:9090/messages")
.then((response) => response.json())
.then((data) => (document.getElementById("heading").innerHTML = data));
</script> -->
</body>
</html>
Loading