Twitter: raymondcamden


Address: Lafayette, LA, USA

Another proof of concept - MockData

12-13-2012 5,612 views Development, JavaScript 6 Comments

Yesterday I whipped up a quick little Node app that I thought folks might find interesting. The basic idea behind it is a quick way to generate fake JSON data. So imagine you are offline and need to write some code that works with an API. You could fire up this app (it runs as its own server) and then simply point your XHR code to the service. By using URL parameters you can define what type of data you would like back. Or maybe you aren't offline but simply don't have access to an API yet. Or perhaps the API you are using has strict usage limits. You get the idea I think. There's already a damn good mocking library out there (MockJax) but I wanted to build something that acted like a real server. Whether or not that make sense is up to debate. Anyway, here's an example.

If I wanted 5 objects with an author field that is a name and an age, I'd do:

http://localhost:3000/?num=5&author=name&age=age

Which could return:

A more complex example could add a gender and a salary, as well as returning a random number of results.

http://localhost:3000/?num=rnd:10&author=name&age=age&gender=oneof:male:female&salary=num:150000

The service uses CORS which means you can be running a virtual host locally and access the URL at localhost with no issues.

Want to play with it? Want to give me feedback on the code? Just hit up the GitHub repo: https://github.com/cfjedimaster/mockdata

6 Comments

  • Commented on 12-13-2012 at 10:52 AM
    Nice Ray. When I need to create fake server side elements, I use Maque (http://maqueapp.com/). It has a lot of nice features that I used to stub out my server calls when I am prototyping.
  • Commented on 12-13-2012 at 11:28 AM
    Nice. I need to start selling mine for 19.99. ;) I like the lorem ipsum generator. I had planned on adding that. Something like

    body=lorem

    or

    bod=lorem:4 (4 paragraphs of lorem ipsum)
  • Commented on 12-13-2012 at 11:39 AM
    Done. :)
  • Commented on 12-13-2012 at 4:19 PM
    Looks good Ray! I personally prefer MockJax because they can be used in unit testing and also doesn't require node. Just curious, what was missing from MockJax?
    http://ja.mesbrown.com/2012/04/adventures-in-mockj...
  • Commented on 12-13-2012 at 4:26 PM
    Not a thing. My thought process was - I don't want to include another JS library - I want to simply switch the URL from "real.com/api" to "tempfornow".
  • Commented on 12-13-2012 at 4:27 PM
    Also remember I'm learning Node, so I'll be building a lot of crap. ;)

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty