Socket
Socket
Sign inDemoInstall

json-server

Package Overview
Dependencies
Maintainers
1
Versions
154
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-server - npm Package Compare versions

Comparing version 0.5.11 to 0.5.12

4

package.json
{
"name": "json-server",
"version": "0.5.11",
"version": "0.5.12",
"description": "Serves JSON files through REST routes.",

@@ -17,3 +17,3 @@ "main": "./src/index.js",

"got": "^1.2.2",
"lowdb": "^0.5.1",
"lowdb": "^0.7.0",
"method-override": "^2.1.2",

@@ -20,0 +20,0 @@ "morgan": "^1.3.1",

@@ -1,23 +0,20 @@

<p align="center">
<img height="56" width="64" src="http://i.imgur.com/QRlAg0b.png"/>
</p>
# 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" }
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
]

@@ -27,93 +24,24 @@ }

Then run `json-server db.json` and go to `http://localhost:3000/posts/1`.
Start JSON Server
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
$ json-server db.json
```
## CLI options
Now if you go to [http://localhost:3000/posts/1](), you will get:
```bash
json-server <source>
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" }
],
"comments": [
{ "id": 1, "body": "baz", "postId": 1 },
{ "id": 2, "body": "qux", "postId": 2 }
]
{
"id": 1,
"body": "foo"
}
```
__JS__
## Routes
```javascript
module.exports = function() {
var data = {};
In fact, you instantly get all these routes:
data.posts = [];
data.posts.push({ id: 1, body: 'foo' });
//...
return data;
}
```
JSON Server expects JS files to export a function that returns an object.
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.
```
GET /posts
GET /posts?title=jsonserver&author=typicode
GET /posts?title=json-server&author=typicode
GET /posts/1/comments

@@ -153,3 +81,3 @@ GET /posts/1

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.

@@ -160,4 +88,64 @@ ```

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

@@ -175,1 +163,5 @@

* [JSON Server GUI](https://github.com/naholyr/json-server-gui)
## License
MIT - [Typicode](https://github.com/typicode)

@@ -15,3 +15,3 @@ var _ = require('underscore')

var db = low()
_.extend(db.object, object)
db.object = object
}

@@ -55,3 +55,3 @@

array = db(req.params.resource).where(function(obj) {
array = db(req.params.resource).filter(function(obj) {
for (var key in obj) {

@@ -63,3 +63,3 @@ var value = obj[key]

}
}).value()
})

@@ -87,3 +87,3 @@ } else {

} else {
array = db(req.params.resource).where(filters).value()
array = db(req.params.resource).filter(filters)
}

@@ -121,3 +121,2 @@ }

.get(+req.params.id)
.value()

@@ -139,3 +138,2 @@ if (resource) {

.insert(req.body)
.value()

@@ -154,3 +152,2 @@ res.jsonp(resource)

.update(+req.params.id, req.body)
.value()

@@ -157,0 +154,0 @@ if (resource) {

@@ -37,2 +37,3 @@ var fs = require('fs')

server.set('json spaces', 2)
server.use(bodyParser.json({limit: '10mb'}))

@@ -53,10 +54,10 @@ server.use(bodyParser.urlencoded({ extended: false }))

server.route('/:resource')
.get(routes.list)
.post(routes.create)
.get(routes.list)
.post(routes.create)
server.route('/:resource/:id')
.get(routes.show)
.put(routes.update)
.patch(routes.update)
.delete(routes.destroy)
.get(routes.show)
.put(routes.update)
.patch(routes.update)
.delete(routes.destroy)

@@ -63,0 +64,0 @@ server.get('/:parent/:parentId/:resource', routes.list)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc