I love prototyping ideas to show others. Quickly making something tangible allows to seek feedback, evaluate the concept, and converge on something useful. The speed is paramount: I want to make something quick before the inspiration is gone, but the prototype should be good enough to get a fair judgement. Lately I have discovered a wonderful pair of tools that complement each other perfectly and allow me to get a functional website up and running in seconds. Ok, minutes.
I have discovered AngularJs data middleware Restangular coupled with json-server.
json-server
Starts small Express-based REST server based on a JSON file, runs under nodejs. Install globally:
sudo npm install -g json-server
Then create a JSON file with a data collection issues.json:
1 | { |
Start the server
1 | json-server issues.json |
The data is updated in memory, the original file is not modified. You can make the data read-only using a command line flag. You can also generate data programmatically by a JavaScript file.
Restangular
Claims to be a perfect fit for any WebApp that consumes data from a RESTful API. Just point it at json-server base url and use the returned objects, all wrapped in promises. CORS and JSONP are supported.
1 | angular.module('project', ['restangular', 'ngRoute']). |
The best feature of this pair is its simplicity. Compared to complete seed projects, such as ultimate-seed, there is almost no code to install, configure or update. Take a look at the example project I have created.
There is just single JSON file, a static HTML page and small JavaScript file mapping user interface actions into REST operations. Extremely simple, easy to understand and debug.
Multiple collections
Single JSON file can host multiple collections like this:
1 | { |
json-server creates separate end point for each top level property
1 | $ json-server combined.json |
Faking data
Suppose you need to generate initial test sets with fake data. A good module to use is Faker. It can generate lots of different types of fake records: from addresses to internet identities, etc. Json-server can use a function to generate data, so both modules work together nicely. I prefer generating and saving fake data to JSON file to have the same data between runs.
1 | var R = require('ramda'); |
This generates a json file with 10 random people. Then we can start the json server
1 | json-server db.json |