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>
24 changes: 24 additions & 0 deletions classwork/server-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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", (request, response) => {
res.send("DELETE Request Called");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to try to use VSCode's code formatting function (press CMD/CTRL + P, then enter "> format" at the prompt box)

Are you deleting an element from the people array? Did you miss capturing the id from url?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for noticing - I've finished it now! Didn't know about CTRL + P + '> format', will learn more about this toll now. Thank you!

});

app.listen(3000, () => {
console.log("listening on port 3000");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assign port number in a variable so you can refer to it in your console message.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Jack, done :)

});
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