Json-server: long running operations

Created on 15 Oct 2015  Â·  6Comments  Â·  Source: typicode/json-server

Is there a way for simulate (forcing) a long running operation?

Most helpful comment

Hi @franciscocpg,

Sure, you can use --delay option when starting json-server

# Will add a 5 seconds delay to responses
json-server db.json --delay 5000

Or with Chrome DevTools, you can simulate slow networks (3G, EDGE, ...).

And finally, you can use the project as a module if you want to tune specific routes:

var jsonServer = require('json-server')

var server = jsonServer.create()

server.use(jsonServer.defaults())

// Add a delay to /posts requests only
server.use('/posts', function(req, res, next) {
  setTimeout(next, 5000)
})

var router = jsonServer.router('db.json')
server.use(router)

server.listen(3000)

All 6 comments

Hi @franciscocpg,

Sure, you can use --delay option when starting json-server

# Will add a 5 seconds delay to responses
json-server db.json --delay 5000

Or with Chrome DevTools, you can simulate slow networks (3G, EDGE, ...).

And finally, you can use the project as a module if you want to tune specific routes:

var jsonServer = require('json-server')

var server = jsonServer.create()

server.use(jsonServer.defaults())

// Add a delay to /posts requests only
server.use('/posts', function(req, res, next) {
  setTimeout(next, 5000)
})

var router = jsonServer.router('db.json')
server.use(router)

server.listen(3000)

hi @typicode.
Thanks for your answer.
In fact I want to simulate for a specific route, so the last solution you provided is what I want.

You're welcome :)

is it possible to use the timeout per route? I tried this but it didn't work

server.post('/my/route', function(req, res, next) {
    setTimeout(next, 10000)
    res.jsonp({})
})

but that didn't actaully delay the reponse

Hi @joshuaswilcox

This should do the trick

server.post('/my/route', function(req, res, next) {
    setTimeout(() => res.jsonp({}), 10000)
})

Ah yes, of course! Thanks

--Josh

Sent from the future

From: typicode notifications@github.com notifications@github.com
Reply: typicode/json-server
reply@reply.github.com
reply@reply.github.com
Date: April 12, 2017 at 4:07:40 PM
To: typicode/json-server json-server@noreply.github.com
json-server@noreply.github.com
CC: Joshua Wilcox jwilcox09@gmail.com jwilcox09@gmail.com, Mention
mention@noreply.github.com mention@noreply.github.com
Subject: Re: [typicode/json-server] long running operations (#188)

Hi @joshuaswilcox https://github.com/joshuaswilcox
>

This should do the trick

server.post('/my/route', function(req, res, next) {
setTimeout(() => res.jsonp({}), 10000)
})

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/typicode/json-server/issues/188#issuecomment-293692412,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAfQTr3b5yVFzemRv6hOpqoiY_ynPEWPks5rvS8MgaJpZM4GPvo9
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pantchox picture pantchox  Â·  3Comments

0plus1 picture 0plus1  Â·  3Comments

boydenhartog picture boydenhartog  Â·  3Comments

TXRRNT picture TXRRNT  Â·  4Comments

shikaan picture shikaan  Â·  3Comments