Giter Club home page Giter Club logo

jserver's Introduction

中文版说明

jserver

A json api and static files server in rust

Just like json-server from typicode (nodejs)

Get a full fake REST API with zero coding in less than 30 seconds (seriously)

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Getting started

Install JServer

cargo build --release
cp target/release/jserver /usr/bin/

Create a data.json file with some data

{
  "posts": [
    { "id": 1, "title": "jserver", "author": "jupiter.gao" }
  ],
  "comments": [
    { "id": 1, "body": "some comment" }
  ],
  "profile": { "name": "jupiter" },
  "homepage": "https://apicenter.com.cn"
}

Start JServer

jserver

Now if you go to http://localhost:2901/api/posts/1, you'll get

{ "id": 1, "title": "jserver", "author": "jupiter.gao" }

Also when doing requests, it's good to know that:

  • If you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to data.json.
  • Your request body JSON should be object or single value, just like the GET output. (for example {"name": "Foobar"} "test string" 83.01)
  • Id values are not mutable. Any id value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.
  • A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will return a 400 status code.

Routes

Based on the previous data.json file, here are all the default routes.

Array routes

GET    /api/posts
GET    /api/posts/1
POST   /api/posts
PUT    /api/posts/1
PATCH  /api/posts/1
DELETE /api/posts/1

Object or Value routes

GET    /api/profile
POST   /api/profile
PUT    /api/profile
PATCH  /api/profile

Filter

GET    /api/posts?title=jserver
GET    /api/posts?id=1

Operators

For numbers, use the following suffix: _lt, _lte, _gt, _gte for <, <=, >, >= respectively. For strings, use _like for contains and _nlike for not contains. For arrays, use _contains for contains and _ncontains for not contains. For numbers, strings, booleans, use _ne for !=. For fields, use _exists for is not null and _nexists for is null.

GET    /api/posts?title_like=server
GET    /api/posts?id_gt=1&id_lt=3&title_exists=true

Paginate

Use optional _page and optional _size to paginate returned data.

GET /api/posts?_page=7
GET /api/posts?_page=7&_size=20
GET /api/posts?_size=100

20 items are returned by default, page is 1 based(0 is treated as 1)

Sort

Add _sort and _order (ascending order by default)

GET /api/posts?_sort=views&_order=asc

For multiple fields, use the following format:

GET /api/posts?_sort=user,views&_order=desc,asc

Slice

Add _start and (_end or _limit)

GET /api/posts?_start=20&_end=30
GET /api/posts?_start=20&_limit=10

An X-Total-Count header is included in the array response

Database

GET /db

Upload files

You can upload files to the server and access them through the static file server below.

POST /upload

Request body should be multipart/form-data and file field name should be file. Response body will be a JSON array with each item having name, path and size properties.

Static file server

You can use JSON Server to serve your HTML, JS and CSS, simply create a ./public directory or use --public-path to set a different static files directory.

mkdir public
echo 'hello world' > public/index.html
jserver
jserver --public-path ./some-other-dir

CLI usage

Usage: jserver [OPTIONS]

Options:
  -b, --bind-address <BIND_ADDRESS>          [default: 0.0.0.0:2901]
  -d, --db-path <DB_PATH>                    [default: ./data.json]
  -p, --public-path <PUBLIC_PATH>            [default: ./public]
  -i, --id <ID>                              [default: id]
  -m, --max-body-limit-m <MAX_BODY_LIMIT_M>  [default: 100]
      --debug
  -h, --help                                 Print help
  -V, --version                              Print version

License

Apache License 2.0

jserver's People

Contributors

jupitergao18 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.