diff --git a/README.md b/README.md index de6e76b..1aff9da 100644 --- a/README.md +++ b/README.md @@ -1,120 +1,48 @@ -

- -

- # JSON Server [![Build Status](https://travis-ci.org/typicode/json-server.svg)](https://travis-ci.org/typicode/json-server) [![NPM version](https://badge.fury.io/js/json-server.svg)](http://badge.fury.io/js/json-server) -> Give it a JSON or JS file and it will serve it through REST routes. +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 flexible back-end for quick prototyping and mocking. +Created with <3 for front-end developers who need a quick back-end for prototyping and mocking. -_Powers [JSONPlaceholder](http://jsonplaceholder.typicode.com)_ +Powers [JSONPlaceholder](http://jsonplaceholder.typicode.com) -## Usage +## Example -### CLI - -Create a `db.json` file: +Create a `db.json` file ```javascript { "posts": [ - { "id": 1, "body": "foo" } - ] -} -``` - -Then run `json-server db.json` and go to `http://localhost:3000/posts/1`. - -You should get `{ "id": 1, "body": "foo" }`. - -### Module - -```javascript -var server = require('json-server'); - -server({ - posts: [ - { id: 1, body: 'foo' } - ] -}).listen(3000); -``` - -## Features - -* Lets you use plain JSON or simple JS file -* Supports __GET__ but also __POST__, __PUT__, __DELETE__ and even __PATCH__ requests -* Can be used from anywhere through __cross domain__ requests (JSONP or CORS) -* Can load remote JSON files ([JSON Generator](http://www.json-generator.com/), ...) -* Can be deployed on Nodejitsu, Heroku, ... - -## Install - -```bash -$ npm install -g json-server -``` - -## CLI options - -```bash -json-server - -Examples: - json-server db.json - json-server file.js - json-server http://example.com/db.json - - -Options: - --help, -h Show help - --version, -v Show version number - --port, -p Set port [default: 3000] -``` - -#### Input - -Here's 2 examples showing how to format JSON or JS seed file: - -__JSON__ - -```javascript -{ - "posts": [ - { "id": 1, "body": "foo" }, - { "id": 2, "body": "bar" } + { "id": 1, "title": "json-server", "author": "typicode" } ], "comments": [ - { "id": 1, "body": "baz", "postId": 1 }, - { "id": 2, "body": "qux", "postId": 2 } + { "id": 1, "body": "some comment", "postId": 1 } ] } ``` -__JS__ +Start JSON Server + +```bash +$ json-server db.json +``` + +Now if you go to [http://localhost:3000/posts/1](), you will get: ```javascript -module.exports = function() { - var data = {}; - - data.posts = []; - data.posts.push({ id: 1, body: 'foo' }); - //... - - return data; +{ + "id": 1, + "body": "foo" } ``` -JSON Server expects JS files to export a function that returns an object. +## Routes -JS files are useful if you need to programmaticaly create a lot of data. - -## Available routes - -Let's say we have `posts`, here's the routes you can use. +In fact, you instantly get all these routes: ``` GET /posts -GET /posts?title=jsonserver&author=typicode +GET /posts?title=json-server&author=typicode GET /posts/1/comments GET /posts/1 POST /posts @@ -149,13 +77,73 @@ Returns database. GET /db ``` -Returns default index file or content of `./public/index.html` (useful if you need to set a custom home page). +Returns default index file or serves `./public` directory. ``` GET / ``` -For more routes usage examples, have a look at [JSONPlaceholder](https://github.com/typicode/jsonplaceholder)'s README. +## Install + +```bash +$ npm install -g json-server +``` + +## Extras + +### Static file server + +You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory. + +### Access from anywhere + +You can access your fake API from anywhere using CORS and JSONP. + +### Remote schema + +You can load remote schemas: + +```bash +$ json-server http://example.com/file.json +$ json-server http://jsonplaceholder.typicode.com/db +``` + +### JS file support + +You can use JS to programmatically create data: + +```javascript +module.exports = function() { + data = { users: [] } + // Create 1000 users + for (var i = 0; i < 1000; i++) { + data.users.push({ name: 'user' + i }) + } + return data +} +``` + +```bash +$ json-server index.js +``` + +### Module + +You can use JSON Server as a module: + +```javascript +var server = require('json-server') + +server({ + posts: [ + { id: 1, body: 'foo' } + ] +}).listen(3000) +``` + +### Deployment + +You can deploy JSON Server. For example, [JSONPlaceholder](http://jsonplaceholder.typicode.com) is an online fake API powered by JSON Server and running on Heroku. ## Links @@ -169,3 +157,7 @@ For more routes usage examples, have a look at [JSONPlaceholder](https://github. * [Grunt JSON Server](https://github.com/tfiwm/grunt-json-server) * [docker-json-server](https://github.com/clue/docker-json-server) * [JSON Server GUI](https://github.com/naholyr/json-server-gui) + +## License + +MIT - [Typicode](https://github.com/typicode)