Fake JSON-api server
Simple json-api server running in browser or node environment. Intercepts client XHR requests for defined routes when running in browser. Useful for test runners and setting up fake backend api server for client applications.
Fake json api server processes data per json:api specification. It can be easily configured to handle (paginated) get, post, put and delete requests for user defined resources. Define dataset, filters and validation rules for each entity. Loaded dataset can be persisted to localStorage in browser.
Examples and api
Create fake server instance with configuration object.
baseApiUrl: '/api' resources: tag: data: type: 'tag' id: '1' attributes: title: 'Tag 1' type: 'tag' id: '2' attributes: title: 'Tag 2' ; $; $;
Persisting data to localStorage, setting up filters and validation rules for post and put request can be defined like so:
baseApiUrl: '/api' storageKey: 'fakeServerStorage' resources: article: filters: { return title >= 0; } validationRules: title: { return titlelength > 0; } message: 'Please enter title.' { return _value; } ;
Node server setup
Following code will run fake json api server on localhost port 3000.
// server.jsvar FakeServer = require 'fake-json-api-server/src/nodeServer'; port: 3000 resources: tag: data: type: 'tag' id: '1' attributes: title: 'Tag 1' type: 'tag' id: '2' attributes: title: 'Tag 2' ;
node server.js
Api is now setuped to serve tag resources in json-api format on http://localhost:3000/tag
Installation
FakeJsonApiServer is packaged as UMD library so you can use it in CommonJS and AMD environment or with browser globals.
npm install fake-json-api-server --save
// with bundlersvar FakeJsonApiServer = ; // with browser globalsvar FakeJsonApiServer = windowFakeJsonApiServer;