Skip to content

Commit b8915e7

Browse files
committed
First commit
0 parents  commit b8915e7

18 files changed

Lines changed: 1091 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎LICENSE‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 typicode
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
![](http://i.imgur.com/dLeJmw6.png)
2+
3+
__JSON Server__
4+
5+
> Give it a JSON or JS seed file and it will serve it through REST routes.
6+
7+
_Created with :heart: for front-end developers who need a flexible back-end for quick prototyping and mocking._
8+
9+
10+
## Examples
11+
12+
### Command line interface
13+
14+
```bash
15+
$ cat db.json
16+
{
17+
posts: [
18+
{ id: 1, body: 'foo' }
19+
]
20+
}
21+
$ json-server --file db.json
22+
$ curl -i http://localhost:3000/posts/1
23+
```
24+
25+
### Node module
26+
27+
```javascript
28+
var server = require('json-server');
29+
30+
var db = {
31+
posts: [
32+
{id: 1, body: 'foo'}
33+
]
34+
}
35+
36+
server.run(db);
37+
```
38+
39+
40+
## Why?
41+
42+
* Supports __GET__ but also __POST__, __PUT__, __DELETE__ and even __PATCH__ requests
43+
* Can be used from anywhere through __cross domain__ requests (JSONP or CORS)
44+
* Can load remote JSON files
45+
* Can be deployed on Nodejitsu, Heroku, ...
46+
47+
48+
## Installation
49+
50+
```bash
51+
$ npm install -g json-server
52+
```
53+
54+
55+
## Usage
56+
57+
### Command line interface
58+
59+
```bash
60+
json-server --help
61+
62+
Usage: json-server [options]
63+
64+
Options:
65+
66+
-h, --help output usage information
67+
-V, --version output the version number
68+
-f --file <file> load db from a js or json file
69+
-u --url <url> load db from a URL
70+
-p --port [port] server port
71+
--read-only read only mode
72+
```
73+
74+
JSON Server can load JSON from multiple sources:
75+
76+
```bash
77+
$ json-server --file db.json
78+
$ json-server --file seed.js
79+
$ json-server --url http://example.com/db.json
80+
```
81+
82+
And be run in read-only mode (useful if deployed on a public server):
83+
84+
```bash
85+
$ json-server --file db.json --read-only
86+
```
87+
88+
#### Input
89+
90+
Here's 2 examples showing how to format JSON or JS seed file:
91+
92+
* __db.json__
93+
```javascript
94+
{
95+
posts: [
96+
{ id: 1, body: 'foo'},
97+
{ id: 2, body: 'bar'}
98+
],
99+
comments: [
100+
{ id: 1, body: 'baz', postId: 1}
101+
{ id: 2, body: 'qux', postId: 2}
102+
]
103+
}
104+
```
105+
106+
* __seed.js__
107+
```javascript
108+
exports.run = function() {
109+
var data = {};
110+
111+
data.posts = [];
112+
data.posts.push({id: 1, body: 'foo'});
113+
//...
114+
115+
return data;
116+
}
117+
```
118+
119+
JSON Server expects JS files to export a ```run``` method that returns an object.
120+
121+
Seed files are useful if you need to programmaticaly create a lot of data.
122+
123+
124+
### Node module
125+
126+
#### run(db, [options])
127+
128+
```javascript
129+
var server = require('json-server'),
130+
db = require('./seed').run();
131+
132+
var options = { port: 4000, readOnly: true };
133+
134+
server.run(db, options);
135+
```
136+
137+
By default, ```port``` is set to 3000 and ```readOnly``` to false.
138+
139+
## Routes
140+
141+
```
142+
GET /:resource
143+
GET /:resource?attr=&attr=&
144+
GET /:parent/:parentId/:resource
145+
GET /:resource/:id
146+
POST /:resource
147+
PUT /:resource/:id
148+
PATCH /:resource/:id
149+
DEL /:resource/:id
150+
```
151+
152+
For routes usage information, have a look at [JSONPlaceholder](https://github.com/typicode/jsonplaceholder) code examples.
153+
154+
```
155+
GET /db
156+
```
157+
158+
Returns database state.
159+
160+
161+
```
162+
GET /
163+
```
164+
165+
Returns default index file or content of ./public/index.html (useful if you need to set a custom home page).
166+
167+
168+
## Support
169+
170+
If you like the project, please tell your friends about it, star it or give feedback :) It's very much appreciated!
171+
172+
For project updates or to get in touch, [@typicode](http://twitter.com/typicde). You can also send me a mail.
173+
174+
## Test
175+
176+
```bash
177+
$ npm install
178+
$ npm test
179+
```

‎bin/cli.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env node
2+
var program = require('commander'),
3+
request = require('superagent'),
4+
url = require('url'),
5+
fs = require('fs'),
6+
server = require('../server'),
7+
logger = require('../utils/logger'),
8+
options = {};
9+
10+
function loadFile(file, cb) {
11+
var path = process.cwd() + '/' + file,
12+
db;
13+
14+
if (/\.json$/.test(file)) db = require(path);
15+
if (/\.js$/.test(file)) db = require(path).run();
16+
17+
cb(db);
18+
}
19+
20+
function loadURL(url, cb) {
21+
logger.info('Fetching ' + url + '...')
22+
request
23+
.get(url)
24+
.end(function(error, res) {
25+
if (error) {
26+
logger.error(error);
27+
} else {
28+
cb(JSON.parse(res));
29+
}
30+
});
31+
}
32+
33+
function onDatabaseLoaded(db) {
34+
server.run(db, options);
35+
}
36+
37+
program
38+
.version('0.1.0')
39+
.option('-f --file <file>', 'load db from a js or json file')
40+
.option('-u --url <url>', 'load db from a URL')
41+
.option('-p --port [port]', 'server port')
42+
.option('--read-only', 'read only mode')
43+
.parse(process.argv);
44+
45+
if (program.port) options.port = program.port;
46+
if (program.readOnly) options.readOnly = true;
47+
if (program.file) loadFile(program.file, onDatabaseLoaded);
48+
if (program.url) loadURL(program.url, onDatabaseLoaded);

‎package.json‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "json-server",
3+
"version": "0.1.0",
4+
"description": "Serves JSON file through REST routes",
5+
"main": "server.js",
6+
"bin": "./bin/cli.js",
7+
"directories": {
8+
"test": "test"
9+
},
10+
"dependencies": {
11+
"commander": "~2.0.0",
12+
"cors": "~2.1.0",
13+
"express": "~3.4.4",
14+
"logan": "~0.0.2",
15+
"request": "~2.27.0",
16+
"underscore": "~1.5.2",
17+
"underscore.inflections": "~0.2.1"
18+
},
19+
"devDependencies": {
20+
"superagent": "~0.15.7",
21+
"supertest": "~0.8.1"
22+
},
23+
"scripts": {
24+
"test": "mocha -R spec test",
25+
"start": "node server.js"
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "git://github.com/typicode/json-server.git"
30+
},
31+
"keywords": [
32+
"JSON",
33+
"server",
34+
"fake",
35+
"REST",
36+
"API",
37+
"prototyping",
38+
"mock",
39+
"mocking",
40+
"test",
41+
"testing",
42+
"rest",
43+
"data",
44+
"dummy"
45+
],
46+
"author": "Typicode <typicode@gmail.com>",
47+
"license": "MIT",
48+
"bugs": {
49+
"url": "https://github.com/typicode/json-server/issues"
50+
},
51+
"homepage": "https://github.com/typicode/json-server"
52+
}

‎public/images/logo.png‎

2.66 KB
Loading

‎public/index.html‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<html>
2+
<head>
3+
<title>JSON Server</title>
4+
<link href="//netdna.bootstrapcdn.com/bootswatch/3.0.2/journal/bootstrap.min.css" rel="stylesheet">
5+
<link rel="stylesheet" href="stylesheets/style.css">
6+
</head>
7+
<body>
8+
<div class="container">
9+
<a href="https://github.com/typicode/json-server" class="logo">
10+
<img src="images/logo.png">
11+
</a>
12+
13+
<p class="alert alert-info">
14+
Congrats! You're successfully running JSON Server.
15+
</p>
16+
17+
<hr>
18+
19+
<h2>Resources</h2>
20+
<p>
21+
<ul id="resources">loading, please wait...</ul>
22+
</p>
23+
24+
<p>
25+
To view database current state:
26+
<ul>
27+
<li>
28+
<a href="db">db</a>
29+
</li>
30+
</ul>
31+
</p>
32+
33+
<h2>Requests</h2>
34+
<p>
35+
Resources can be accessed in various ways.
36+
</p>
37+
<p>
38+
JSON Server supports:
39+
<ul>
40+
<li>GET, POST, PUT, PATCH, DESTROY and OPTIONS verbs.</li>
41+
<li>JSONP or CORS cross domain requests.</li>
42+
</ul>
43+
</p>
44+
45+
<h2>Documentation</h2>
46+
<p>
47+
View
48+
<a href="http://github.com/typicode/jsonserver">README</a>
49+
on GitHub.
50+
</p>
51+
52+
<h2>Issues</h2>
53+
<p>Please go
54+
<a href="https://github.com/typicode/jsonplaceholder/issues">here</a>.
55+
</p>
56+
57+
<hr>
58+
59+
<p>
60+
<i>To customize this page, just create a ./public/index.html file.</i>
61+
</p>
62+
</div>
63+
64+
65+
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
66+
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
67+
<script>
68+
$(function() {
69+
$.get('db').then(function(data) {
70+
$('#resources').empty();
71+
$.each(data, function(key, value) {
72+
$('#resources')
73+
.append('<li><a href="/'+ key + '">' + key + '</a></li>');
74+
})
75+
})
76+
})
77+
</script>
78+
</body>
79+
</html>

0 commit comments

Comments
 (0)