Skip to content
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

How can I get random data #70

Closed
iulo opened this issue Apr 30, 2015 · 9 comments
Closed

How can I get random data #70

iulo opened this issue Apr 30, 2015 · 9 comments

Comments

@iulo
Copy link

iulo commented Apr 30, 2015

I would like some random data.
I creat db.js:

module.exports = function() {
  data = {  };

  data.id = Math.floor( Math.random()*9+1 );
  return data;
};

but still return old data
What should I do ?

@dvidsilva
Copy link

are you running your script like

$ json-server db.js

@typicode
Copy link
Owner

typicode commented May 8, 2015

You need to create it first:

// db.js
module.exports = function() {
  data = { users: [] }
  // Create 1000 users
  for (var i = 0; i < 1000; i++) {
    data.users.push({ id: i, name: 'user' + i })
  }
  return data
}

You can also use modules like https://github.com/Marak/faker.js or https://github.com/boo1ean/casual to create random data.

@crswll
Copy link

crswll commented May 15, 2015

I've been using mockaroo.com for random data. Nice little UI if you feel like getting something quick and realistic.

@typicode
Copy link
Owner

Good point about mockaroo 👍 There's json-generator that seems pretty popular among JSON Server users too.

By the way, if the service offers a URL for the schema, you don't even need to download the file. JSON Server can load it:

json-server http://beta.json-generator.com/api/json/get/FMMNIRS
json-server http://dropbox.com/...
...

@blvz
Copy link

blvz commented May 30, 2015

Can I suggest chance.js? I think it's pretty awesome.

@typicode
Copy link
Owner

Didn't know chance.js, cool stuff (I've added it to the README alongside other node generators)

@scottsmith8
Copy link

I was wondering if its possible to have random data generated for each request? I have copied the random data example provided and added faker, however each time I hit the mock API the same data is returned.

Am I missing something? I just want to simulate polling a server where the data changes.

@dvidsilva
Copy link

I think you'd have to change it here:
https://github.com/typicode/json-server/blob/master/src/server/router/index.js

  // GET /db
  router.get('/db', (req, res) => {
    res.jsonp(db.getState())
  })

When get happens it returns the state of the db, that is set at start time. You'd have to reinitialize the db at every request.

@scottsmith8
Copy link

thanks for the reply, I was playing around with that file and the router.get() method is hit when you load up the JSON Server Index page (eg http://localhost:3000/) not when i hit the route (eg. http://localhost:3000/ramp) .

Not sure I follow your approach?

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

No branches or pull requests

6 participants