From a19073235a65a8278ff262df84f87f155ae9208f Mon Sep 17 00:00:00 2001
From: Joe Enos
Date: Sat, 28 Sep 2024 20:14:07 -0700
Subject: [PATCH 1/3] Adding a request delay
---
public/guide.html | 6 ++++++
public/index.html | 10 +++++++++-
src/app.js | 11 +++++++++++
templates/GUIDE.md | 7 +++++++
templates/index.html | 8 ++++++++
test/app.js | 25 +++++++++++++++++++++++++
6 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/public/guide.html b/public/guide.html
index ca2ee02..a887791 100644
--- a/public/guide.html
+++ b/public/guide.html
@@ -181,6 +181,12 @@ Nested resources
https://jsonplaceholder.typicode.com/users/1/todos
https://jsonplaceholder.typicode.com/users/1/posts
+Delay
+With any request, add a delay=N query string parameter to simulate a slow-running API response.
+
diff --git a/public/index.html b/public/index.html
index 3e2d875..5ac6c1f 100644
--- a/public/index.html
+++ b/public/index.html
@@ -248,7 +248,15 @@ Routes
Note: you can view detailed examples
- here.
+ here.
+
+
+
+ Simulate a delay
+
+
+ You can simulate a delay by adding a
+ delay query parameter (measured in milliseconds) to any route.
diff --git a/src/app.js b/src/app.js
index d734af4..6e15cf6 100644
--- a/src/app.js
+++ b/src/app.js
@@ -5,6 +5,17 @@ const data = require('../data.json')
const app = jsonServer.create()
const router = jsonServer.router(clone(data), { _isFake: true })
+app.use((req, res, next) => {
+ if (req.query.delay) {
+ const delay = parseInt(req.query.delay, 10)
+ if (delay > 0) {
+ setTimeout(next, delay)
+ return
+ }
+ }
+ next()
+})
+
app.use((req, res, next) => {
if (req.path === '/') return next()
router.db.setState(clone(data))
diff --git a/templates/GUIDE.md b/templates/GUIDE.md
index 5563566..681f8f6 100644
--- a/templates/GUIDE.md
+++ b/templates/GUIDE.md
@@ -163,4 +163,11 @@ Available nested routes:
* https://jsonplaceholder.typicode.com/users/1/todos
* https://jsonplaceholder.typicode.com/users/1/posts
+### Delay
+
+With any request, add a delay=N query string parameter to simulate a slow-running API response.
+
+* https://jsonplaceholder.typicode.com/posts/1/comments?delay=500
+* https://jsonplaceholder.typicode.com/posts?userId=1&delay=1000
+
diff --git a/templates/index.html b/templates/index.html
index 291ad22..808883f 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -198,6 +198,14 @@ Routes
here.
+
+ Simulate a delay
+
+
+ You can simulate a delay by adding a
+ delay query parameter (measured in milliseconds) to any route.
+
+
Use your own data
diff --git a/templates/index.html b/templates/index.html
index 808883f..ae14ae1 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -195,7 +195,7 @@ Routes
Note: you can view detailed examples
- here.
+ here.
From a2b527cf567ccf6a7afeff59a0c9e3820b96aa3b Mon Sep 17 00:00:00 2001
From: Joe Enos
Date: Sat, 28 Sep 2024 20:21:41 -0700
Subject: [PATCH 3/3] Adding trailing slash.
---
public/index.html | 2 +-
templates/index.html | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/public/index.html b/public/index.html
index d9b6b90..265a419 100644
--- a/public/index.html
+++ b/public/index.html
@@ -248,7 +248,7 @@ Routes
Note: you can view detailed examples
- here.
+ here.
diff --git a/templates/index.html b/templates/index.html
index ae14ae1..46eddff 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -195,7 +195,7 @@ Routes
Note: you can view detailed examples
- here.
+ here.