svrx-plugin-json-server

1.0.1 • Public • Published

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: {
        filter: '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

Package Sidebar

Install

npm i svrx-plugin-json-server

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

7.41 kB

Total Files

6

Last publish

Collaborators

  • leeluolee