Code Monkey home page Code Monkey logo

Comments (9)

dvidsilva avatar dvidsilva commented on May 22, 2024

are you running your script like

$ json-server db.js

from json-server.

typicode avatar typicode commented on May 22, 2024

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.

from json-server.

crswll avatar crswll commented on May 22, 2024

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

from json-server.

typicode avatar typicode commented on May 22, 2024

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/...
...

from json-server.

blvz avatar blvz commented on May 22, 2024

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

from json-server.

typicode avatar typicode commented on May 22, 2024

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

from json-server.

scottsmith8 avatar scottsmith8 commented on May 22, 2024

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.

from json-server.

dvidsilva avatar dvidsilva commented on May 22, 2024

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.

from json-server.

scottsmith8 avatar scottsmith8 commented on May 22, 2024

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?

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.