Another proof of concept - MockData

This post is more than 2 years old.

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

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Chris Griffith posted on 12/13/2012 at 9:52 PM

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.

Comment 2 by Raymond Camden posted on 12/13/2012 at 10:28 PM

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)

Comment 3 by Raymond Camden posted on 12/13/2012 at 10:39 PM

Done. :)

Comment 4 by James Brown posted on 12/14/2012 at 3:19 AM

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

Comment 5 by Raymond Camden posted on 12/14/2012 at 3:26 AM

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

Comment 6 by Raymond Camden posted on 12/14/2012 at 3:27 AM

Also remember I'm learning Node, so I'll be building a lot of crap. ;)