Home

Awesome

svrx-plugin-json-server

svrx npm

The svrx plugin for json-server, help us to get a full fake REST API with zero coding and integrated with svrx.

Usage

Please make sure that you have installed svrx already.

Example beblow need a db.json in your current working directory, and with content like

{
  posts: [
    { id: 1, title: 'json-server', author: 'typicode' },
    { id: 2, title: 'json-server', author: 'typicode2' }
  ]
}

Via CLI

svrx -p json-server

Via API

const svrx = require('@svrx/svrx');

svrx({ plugins: ['json-server'] }).start();

Then Play it in your route.js

This plugin will register a route action named jsonServer which can help to proxy specified request to json-server

see svrx routing dsl(中文文档) for more details

get('/(.*)').to.jsonServer();

Now, all request will be proxy to json-server.

Visit page /posts/1 in your browser, you will
found that the corresponding post object has been output, like

  { id: 1, title: 'json-server', author: 'typicode' },

request rewrite

By action rewrite, you can also rewrite request before proxy to json-server

get('/api/(.*)').rewrite('/{0}')to.jsonServer()
// /api/posts >  /posts

And More...

See official json-server reference for more details about json-server

Options

source [String|Object]:

The source of json server , can be an object or string, default is db.json;

svrx({
  plugins: [
    {
      name: 'json-server',
      options: {
        //or source: 'path/to/db.json'
        source: {
          posts: [
            { id: 1, title: 'json-server', author: 'typicode' },
            { id: 2, title: 'json-server', author: 'typicode2' }
          ]
        }
      }
    }
  ]
});

proxy [String|RegExp|Function]:

A regular expression string or filter function that represents a matching rule to proxy request to json-server.

svrx({
  plugins: [
    {
      name: 'json-server',
      options: {
        proxy: 'blog|post' // blog and post will go through json-server
      }
    }
  ]
});

In a simple usage, you can use this option instead of manually declaring a route.

logger [Object]:

Enable json-server logger (default: false)

License

MIT