Skip to content

12: http requests and responses handling #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

aimxnaim
Copy link
Owner

No description provided.

- Added main application component with header and main sections.
- Created AvailablePlaces and UserPlaces components for displaying places.
- Implemented PlacesContainer for structured layout of places.
- Defined Place model to represent place data.
- Developed styles for components to enhance UI.
- Set up service for managing user places.
- Configured main entry point and index HTML for application bootstrapping.
@aimxnaim aimxnaim self-assigned this May 12, 2025
@aimxnaim aimxnaim linked an issue May 12, 2025 that may be closed by this pull request
Comment on lines +21 to +29
app.get("/places", async (req, res) => {
await new Promise((resolve) => setTimeout(resolve, 3000));

const fileContent = await fs.readFile("./data/places.json");

const placesData = JSON.parse(fileContent);

res.status(200).json({ places: placesData });
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.

Copilot Autofix

AI 3 months ago

To address the issue, we will introduce rate limiting to the application using the express-rate-limit package. This middleware will limit the number of requests a client can make to the server within a specified time window. Specifically, we will:

  1. Install the express-rate-limit package.
  2. Configure a rate limiter with a reasonable limit (e.g., 100 requests per 15 minutes).
  3. Apply the rate limiter middleware globally to all routes, ensuring that all endpoints, including /places, are protected.

This approach ensures that the application is safeguarded against excessive requests while maintaining its functionality.


Suggested changeset 2
12-http/starting-project/backend/app.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/12-http/starting-project/backend/app.js b/12-http/starting-project/backend/app.js
--- a/12-http/starting-project/backend/app.js
+++ b/12-http/starting-project/backend/app.js
@@ -4,5 +4,5 @@
 import express from "express";
+import RateLimit from "express-rate-limit";
 
 const app = express();
-
 app.use(express.static("images"));
@@ -10,2 +10,9 @@
 
+// Rate limiting: maximum of 100 requests per 15 minutes
+const limiter = RateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // limit each IP to 100 requests per windowMs
+});
+
+app.use(limiter);
 // CORS
EOF
@@ -4,5 +4,5 @@
import express from "express";
import RateLimit from "express-rate-limit";

const app = express();

app.use(express.static("images"));
@@ -10,2 +10,9 @@

// Rate limiting: maximum of 100 requests per 15 minutes
const limiter = RateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
});

app.use(limiter);
// CORS
12-http/starting-project/backend/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/12-http/starting-project/backend/package.json b/12-http/starting-project/backend/package.json
--- a/12-http/starting-project/backend/package.json
+++ b/12-http/starting-project/backend/package.json
@@ -14,3 +14,4 @@
     "body-parser": "^1.20.2",
-    "express": "^4.18.2"
+    "express": "^4.18.2",
+    "express-rate-limit": "^7.5.0"
   }
EOF
@@ -14,3 +14,4 @@
"body-parser": "^1.20.2",
"express": "^4.18.2"
"express": "^4.18.2",
"express-rate-limit": "^7.5.0"
}
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 7.5.0 None
Copilot is powered by AI and may make mistakes. Always verify output.
Comment on lines +31 to +37
app.get("/user-places", async (req, res) => {
const fileContent = await fs.readFile("./data/user-places.json");

const places = JSON.parse(fileContent);

res.status(200).json({ places });
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.

Copilot Autofix

AI 3 months ago

Copilot could not generate an autofix suggestion

Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.

Comment on lines +39 to +62
app.put("/user-places", async (req, res) => {
const placeId = req.body.placeId;

const fileContent = await fs.readFile("./data/places.json");
const placesData = JSON.parse(fileContent);

const place = placesData.find((place) => place.id === placeId);

const userPlacesFileContent = await fs.readFile("./data/user-places.json");
const userPlacesData = JSON.parse(userPlacesFileContent);

let updatedUserPlaces = userPlacesData;

if (!userPlacesData.some((p) => p.id === place.id)) {
updatedUserPlaces = [...userPlacesData, place];
}

await fs.writeFile(
"./data/user-places.json",
JSON.stringify(updatedUserPlaces)
);

res.status(200).json({ userPlaces: updatedUserPlaces });
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.
This route handler performs
a file system access
, but is not rate-limited.
This route handler performs
a file system access
, but is not rate-limited.

Copilot Autofix

AI 3 months ago

Copilot could not generate an autofix suggestion

Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.

Comment on lines +64 to +84
app.delete("/user-places/:id", async (req, res) => {
const placeId = req.params.id;

const userPlacesFileContent = await fs.readFile("./data/user-places.json");
const userPlacesData = JSON.parse(userPlacesFileContent);

const placeIndex = userPlacesData.findIndex((place) => place.id === placeId);

let updatedUserPlaces = userPlacesData;

if (placeIndex >= 0) {
updatedUserPlaces.splice(placeIndex, 1);
}

await fs.writeFile(
"./data/user-places.json",
JSON.stringify(updatedUserPlaces)
);

res.status(200).json({ userPlaces: updatedUserPlaces });
});

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.
This route handler performs
a file system access
, but is not rate-limited.

Copilot Autofix

AI 3 months ago

To address the issue, we will introduce rate limiting to the application using the express-rate-limit package. This middleware will limit the number of requests a client can make to the server within a specified time window. Specifically, we will:

  1. Install the express-rate-limit package.
  2. Configure a rate limiter with appropriate settings (e.g., a maximum of 100 requests per 15 minutes).
  3. Apply the rate limiter middleware globally to all routes to ensure consistent protection across the application.

This approach ensures that all endpoints, including the one flagged by CodeQL, are protected against excessive requests.

Suggested changeset 2
12-http/starting-project/backend/app.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/12-http/starting-project/backend/app.js b/12-http/starting-project/backend/app.js
--- a/12-http/starting-project/backend/app.js
+++ b/12-http/starting-project/backend/app.js
@@ -4,5 +4,5 @@
 import express from "express";
+import rateLimit from "express-rate-limit";
 
 const app = express();
-
 app.use(express.static("images"));
@@ -10,2 +10,8 @@
 
+// Rate limiting
+const limiter = rateLimit({
+  windowMs: 15 * 60 * 1000, // 15 minutes
+  max: 100, // Limit each IP to 100 requests per windowMs
+});
+app.use(limiter);
 // CORS
EOF
@@ -4,5 +4,5 @@
import express from "express";
import rateLimit from "express-rate-limit";

const app = express();

app.use(express.static("images"));
@@ -10,2 +10,8 @@

// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
});
app.use(limiter);
// CORS
12-http/starting-project/backend/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/12-http/starting-project/backend/package.json b/12-http/starting-project/backend/package.json
--- a/12-http/starting-project/backend/package.json
+++ b/12-http/starting-project/backend/package.json
@@ -14,3 +14,4 @@
     "body-parser": "^1.20.2",
-    "express": "^4.18.2"
+    "express": "^4.18.2",
+    "express-rate-limit": "^7.5.0"
   }
EOF
@@ -14,3 +14,4 @@
"body-parser": "^1.20.2",
"express": "^4.18.2"
"express": "^4.18.2",
"express-rate-limit": "^7.5.0"
}
This fix introduces these dependencies
Package Version Security advisories
express-rate-limit (npm) 7.5.0 None
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Chapter 12: Sending HTTP Requests & Handling Responses
1 participant