Comparing json-server versus dyson mock server

Posted in Uncategorized

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Last tutorial, we implemented json-server with movies data. Let’s see how that compares with implementing the same thing with dyson mock server.

Get dyson installed globally (so that it can run on the command line) just like we did for json-server…

npm install -g dyson

Instead of using movies.json, in dyson we use Javascript file “movies.js” which like Node is a exported module where we export an object containing a “path” and a “template” …

What that means is that if user hits the “path” endpoint, the “template” data will be returned.

Now start up dyson mock server of port 3001…

dyson api 3001

Point browser to …

http://localhost:3001/backend/rest/movies

to see the results of the template data…

This is a get request and typically you would put the “movies.js” file in a sub-folder named “get” to let dyson know that this is a GET request as opposed to PUT.

So we move the “movies.js” into the “get” sub-folder like so…

It works just as before. Alternatively, you can specify the method “GET” in the exported object like so…

json-server versus dyson in queries

Whereas in json-server, queries, sort, and order would work right out of the box like …

http://localhost:3001/backend/rest/movies?year=1977

For dyson, it requires a bit of work.

First let’s get dyson to return movie with id of 2 when this URL is hit: http://localhost:3001/backend/rest/movies/2

We save the movies data array in a separate file called data.js like this …

And then movies.js can require it like this …

Next we add a second object with the “id” parameter in the path URL …

The template becomes a function which returns the specific data object that we are seeking. We seek this item using the Javascript “filter” method on the movies array. Note that param.id is a string because it came in from the URL. We need to convert that to a number because the id in our movie data.js is a numeric. Using the Number function to convert the URL input to the numeric type is also a good security measure. Do not trust the user inputs from an URL.

Comparison

I find that json-server is easier to learn and get up and running with basic functionality (such as queries) already in place. dyson mock server requires a bit more work and has a steeper learning curve.

As of time of this writing, json-server has a lot more weekly downloads than dyson.

For basic functionality, I think both are equally capable. For advanced functionality, I don’t know because we have not explored those topics.


Related Posts

Tags

Share This