Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions chat-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"start": "node server.js"
},
"dependencies": {
"cors": "^2.8.5"
"cors": "^2.8.5",
"express": "^4.18.2"
},
"repository": {
"url": "https://glitch.com/edit/#!/hello-express"
Expand All @@ -20,5 +21,6 @@
"node",
"glitch",
"express"
]
],
"author": ""
}
48 changes: 42 additions & 6 deletions chat-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,58 @@ const cors = require("cors");
const app = express();

app.use(cors());
app.use(express.json());

const welcomeMessage = {
id: 0,
from: "Bart",
text: "Welcome to CYF chat system!",
from: "Onur Atas",
text: "Welcome to my chat system!",
};

//This array is our "data store".
//We will start with one message in the array.
//Note: messages will be lost when Glitch restarts our server.
const messages = [welcomeMessage];

app.get("/", function (request, response) {
response.sendFile(__dirname + "/index.html");
});

app.listen(process.env.PORT,() => {
app.post("/messages", (req, res) => {
const { from, text } = req.body;
if (!from || !text) {
return res.status(400).json({ error: 'Both "from" and "text" fields are required.' });
}

const newMessage = {
id: messages.length,
from,
text,
};

messages.push(newMessage);
res.status(201).json(newMessage);
});

app.get("/messages", (req, res) => {
res.json(messages);
});

app.get("/messages/:id", (req, res) => {
const messageId = parseInt(req.params.id);
const message = messages.find((msg) => msg.id === messageId);
if (!message) {
return res.status(404).json({ error: 'Message not found.' });
}
res.json(message);
});

app.delete("/messages/:id", (req, res) => {
const messageId = parseInt(req.params.id);
const index = messages.findIndex((msg) => msg.id === messageId);
if (index === -1) {
return res.status(404).json({ error: 'Message not found.' });
}
messages.splice(index, 1);
res.status(204).send();
});
app.listen(process.env.PORT, () => {
console.log(`listening on PORT ${process.env.PORT}...`);
});
35 changes: 33 additions & 2 deletions hotel-bookings-api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,45 @@ const app = express();
app.use(express.json());
app.use(cors());

//Use this array as your (in-memory) data store.

const bookings = require("./bookings.json");

app.get("/", function (request, response) {
response.send("Hotel booking server. Ask for /bookings, etc.");
});

// TODO add your routes and helper functions here
app.post("/bookings", function (req, res) {
const newBooking = req.body;
const bookingId = generateUniqueId();
newBooking.id = bookingId;
bookings.push(newBooking);
res.status(201).json(newBooking);
});

app.get("/bookings", function (req, res) {
res.json(bookings);
});

app.get("/bookings/:id", function (req, res) {
const bookingId = req.params.id;
const booking = bookings.find((b) => b.id === bookingId);
if (!booking) {
res.status(404).json({ error: "Booking not found" });
} else {
res.json(booking);
}
});

app.delete("/bookings/:id", function (req, res) {
const bookingId = req.params.id;
const index = bookings.findIndex((b) => b.id === bookingId);
if (index === -1) {
res.status(404).json({ error: "Booking not found" });
} else {
bookings.splice(index, 1);
res.sendStatus(204);
}
});

const listener = app.listen(process.env.PORT, function () {
console.log("Your app is listening on port " + listener.address().port);
Expand Down
52 changes: 52 additions & 0 deletions mailing-list-api/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
const mailingLists = require("./mailing-lists");

app.use(express.json());

const lists = new Map(Object.entries(mailingLists));

app.get("/lists", (req, res) => {
const listNames = Array.from(lists.keys());
res.status(200).json(listNames);
});

app.get("/lists/:name", (req, res) => {
const listName = req.params.name;
const members = lists.get(listName);

if (members) {
res.status(200).json({ name: listName, members });
} else {
res.status(404).json({ error: "List not found" });
}
});

app.delete("/lists/:name", (req, res) => {
const listName = req.params.name;

if (lists.has(listName)) {
lists.delete(listName);
res.status(200).send("List deleted successfully");
} else {
res.status(404).json({ error: "List not found" });
}
});

app.put("/lists/:name", (req, res) => {
const listName = req.params.name;
const { members } = req.body;

if (lists.has(listName)) {
lists.set(listName, members);
res.status(200).send("List updated successfully");
} else {
lists.set(listName, members);
res.status(201).send("List created successfully");
}
});

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 24 additions & 18 deletions quote-server/server.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
// server.js
// This is where your node app starts

//load the 'express' module which makes writing webservers easy
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;

//load the quotes JSON
const quotes = require("./quotes.json");
const quotes = require("./quotes.js");

// Now register handlers for some routes:
// / - Return some helpful welcome info (text)
// /quotes - Should return all quotes (json)
// /quotes/random - Should return ONE quote (json)
app.get("/", function (request, response) {
response.send("Neill's Quote Server! Ask me for /quotes/random, or /quotes");
response.send("Onur Atas's Quote Server! Ask for /quotes/random, /quotes, or /quotes/search?term=yourterm");
});

//START OF YOUR CODE...
app.get("/quotes", function (request, response) {
response.json(quotes);
});

//...END OF YOUR CODE
app.get("/quotes/random", function (request, response) {
const randomQuote = pickFromArray(quotes);
response.json(randomQuote);
});

app.get("/quotes/search", function (request, response) {
const searchTerm = request.query.term;
const matchingQuotes = searchQuotes(searchTerm);
response.json(matchingQuotes);
});

//You can use this function to pick one element at random from a given array
//example: pickFromArray([1,2,3,4]), or
//example: pickFromArray(myContactsArray)
//
function pickFromArray(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}

//Start our server so that it listens for HTTP requests!
const listener = app.listen(process.env.PORT, function () {
function searchQuotes(term) {
return quotes.filter((quote) =>
quote.quote.toLowerCase().includes(term.toLowerCase()) ||
quote.author.toLowerCase().includes(term.toLowerCase())
);
}

const listener = app.listen(port, function () {
console.log("Your app is listening on port " + listener.address().port);
});