The svrx plugin for json-server, help us to get a full fake REST API with zero coding and integrated with svrx.
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' } ] }
svrx -p json-server
const svrx = require('@svrx/svrx');
svrx({ plugins: ['json-server'] }).start();
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' },
By action rewrite, you can also rewrite request before proxy to json-server
get('/api/(.*)').rewrite('/{0}')to.jsonServer()
// /api/posts > /posts
See official json-server reference for more details about json-server
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' }
]
}
}
}
]
});
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.
Enable json-server logger (default: false)
MIT