· articles · 2 min read
By Ankit JainWhat Happened to the `--delay` option in JSON-Server?
Why JSON Server’s `--delay` option was removed, its impact, and alternative ways to simulate response delays, like using older versions, browser throttling, or custom middleware solutions.
Recently, developers using JSON Server noticed that the —delay option was removed, which had previously allowed response delays for simulating slow networks. This change has created confusion and challenges for users trying to test latency-related behaviors.
Why Was --delay
Removed?
Yes, the --delay
option was removed in recent updates. The JSON Server maintainer tells to use the browser’s network throttling tools for delay simulation instead. However, this approach applies delays to all resources and only for web-browser testing, making it less useful than the original --delay
option.
Many developers have expressed frustration with its removal and this decision of the maintainers. This removal requires additional steps. This delay functionality baked into json-server for so long, allowed them to bundle this behavior directly, eliminating the need for manual configuration or extra steps. When something is working, why break it? Folks are arguing that removing this feature makes JSON Server less convenient choice for real-world testing scenarios and they started looking for alternatives.
Workarounds for Delays
Browser Throttling
Adjust the network speed via developer tools, but this delays all resources.
Use Older JSON Server Versions
Install an older version (e.g., 0.17.3) that supports --delay
.
npm i json-server@0.17.3
Custom Middleware
Create a custom Express middleware to introduce delays, providing more flexibility and control over specific endpoints.
Here’s a simple custom middleware in Node.js to add a delay:
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
// Custom delay middleware
server.use((req, res, next) => {
const delay = 2000; // 2-second delay
setTimeout(() => next(), delay);
});
server.use(middlewares);
server.use(router);
server.listen(3000, () => {
console.log('JSON Server is running with a delay');
});
This adds a 2-second delay to all requests. Adjust the delay as needed!
Conclusion
The removal of --delay
affects many developers for API simulation workflows, workarounds like browser throttling, downgrading versions, or using middleware can help achieve similar outcomes.
For more details, check the discussion here.