Skip to content

Commit 6d88075

Browse files
committed
Refactor and update to LowDB 0.4
1 parent 9c17780 commit 6d88075

File tree

13 files changed

+370
-400
lines changed

13 files changed

+370
-400
lines changed

README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _Powers http://jsonplaceholder.typicode.com_
1616

1717
```javascript
1818
// db.json
19-
{
19+
{
2020
"posts": [
2121
{ "id": 1, "body": "foo" }
2222
]
@@ -33,13 +33,11 @@ $ curl -i http://localhost:3000/posts/1
3333
```javascript
3434
var server = require('json-server');
3535

36-
server.low.db = {
36+
server({
3737
posts: [
3838
{ id: 1, body: 'foo' }
3939
]
40-
}
41-
42-
server.listen(3000);
40+
}).listen(3000);
4341
```
4442

4543
You can find a running demo here: http://jsonplaceholder.typicode.com.
@@ -62,20 +60,18 @@ $ npm install -g json-server
6260
## CLI usage
6361

6462
```bash
63+
json-server <source>
6564

66-
Usage: json-server <source> [options]
67-
68-
Options:
69-
70-
--version output version
71-
--port <port> set port
65+
Examples:
66+
json-server db.json
67+
json-server file.js
68+
json-server http://example.com/db.json
7269

73-
Exemples:
7470

75-
json-server db.json
76-
json-server seed.js
77-
json-server http://example.com/db.json
78-
71+
Options:
72+
--help, -h Show help
73+
--version, -v Show version number
74+
--port, -p Set port [default: 3000]
7975
```
8076

8177
#### Input
@@ -97,10 +93,10 @@ Here's 2 examples showing how to format JSON or JS seed file:
9793
}
9894
```
9995

100-
* __seed.js__
96+
* __file.js__
10197

10298
```javascript
103-
exports.run = function() {
99+
module.exports = function() {
104100
var data = {};
105101

106102
data.posts = [];
@@ -111,7 +107,7 @@ exports.run = function() {
111107
}
112108
```
113109

114-
JSON Server expects JS files to export a ```run``` method that returns an object.
110+
JSON Server expects JS files to export a function that returns an object.
115111

116112
Seed files are useful if you need to programmaticaly create a lot of data.
117113

@@ -159,4 +155,4 @@ Returns default index file or content of ./public/index.html (useful if you need
159155
## Links
160156

161157
* [Fast prototyping using Restangular and Json-server](http://bahmutov.calepin.co/fast-prototyping-using-restangular-and-json-server.html)
162-
* [Grunt plugin](https://github.com/tfiwm/grunt-json-server)
158+
* [grunt plugin](https://github.com/tfiwm/grunt-json-server)

bin/index.js

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,74 @@
11
#!/usr/bin/env node
2-
var minimist = require('minimist')
32
var updateNotifier = require('update-notifier')
4-
var cli = require('../src/cli')
5-
6-
var notifier = updateNotifier({packagePath: '../package'})
7-
if (notifier.update) notifier.notify()
8-
9-
var argv = minimist(process.argv.slice(2), {
10-
boolean: ["silent"],
11-
alias: {
12-
'p': 'port',
13-
's': 'silent'
14-
},
15-
default: {
16-
silent: false
3+
var _db = require('underscore-db')
4+
var yargs = require('yargs')
5+
var chalk = require('chalk')
6+
var got = require('got')
7+
var pkg = require('../package.json')
8+
var server = require('../src')
9+
10+
updateNotifier({packageName: pkg.name, packageVersion: pkg.version}).notify()
11+
12+
var argv = yargs
13+
.usage('$0 <source>')
14+
.help('help').alias('help', 'h')
15+
.version(pkg.version, 'version').alias('version', 'v')
16+
.options({
17+
port: {
18+
alias: 'p',
19+
description: 'Set port',
20+
default: 3000
21+
}
22+
})
23+
.example('$0 db.json', '')
24+
.example('$0 file.js', '')
25+
.example('$0 http://example.com/db.json', '')
26+
.require(1, 'Missing <source> argument')
27+
.argv
28+
29+
function start(object, filename) {
30+
for (var prop in object) {
31+
console.log('http://localhost:' + port + '/' + chalk.green(prop))
32+
}
33+
34+
console.log(
35+
'\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n'
36+
)
37+
38+
process.stdin.resume()
39+
process.stdin.setEncoding('utf8')
40+
process.stdin.on('data', function (chunk) {
41+
if (chunk.trim().toLowerCase() === 's') {
42+
var file = 'db-' + Date.now() + '.json'
43+
_db.save(object, filename)
44+
console.log('\nSaved snapshot to ' + chalk.green(file) + '\n')
1745
}
18-
})
46+
})
47+
48+
server(object, filename).listen(port)
49+
}
50+
51+
var source = argv._[0]
52+
var port = process.env.PORT || argv.port
53+
54+
console.log(chalk.green('\n{^ ^} Yo!\n'))
55+
console.log('Loading database from ' + source + '\n')
56+
57+
if (/\.json$/.test(source)) {
58+
var filename = process.cwd() + '/' + source
59+
var object = require(filename)
60+
start(object, filename)
61+
}
62+
63+
if (/\.js$/.test(source)) {
64+
var object = require(process.cwd() + '/' + source)()
65+
start(object)
66+
}
1967

20-
cli.run(argv)
68+
if (/^http/.test(source)) {
69+
got(source, function(err, data) {
70+
if (err) throw err
71+
var object = JSON.parse(data)
72+
start(object)
73+
})
74+
}

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
"chalk": "^0.4.0",
1313
"cors": "^2.3.0",
1414
"errorhandler": "^1.2.0",
15-
"express": "^4.9.0",
16-
"lowdb": "^0.3.0",
15+
"express": "^4.9.5",
16+
"got": "^1.2.2",
17+
"lowdb": "^0.4.2",
1718
"method-override": "^2.1.2",
18-
"minimist": "0.0.8",
1919
"morgan": "^1.3.1",
2020
"serve-static": "^1.6.1",
21-
"superagent": "~0.15.7",
22-
"underscore": "~1.5.2",
21+
"superagent": "^0.15.7",
22+
"underscore": "^1.5.2",
23+
"underscore-db": "^0.8.0",
2324
"underscore.inflections": "~0.2.1",
24-
"update-notifier": "^0.1.8",
25-
"yargs": "^1.2.1"
25+
"update-notifier": "^0.2.2",
26+
"yargs": "^1.3.1"
2627
},
2728
"devDependencies": {
2829
"supertest": "~0.8.1",

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h4>Documentation</h4>
5252
</p>
5353

5454
<h4>Issues</h4>
55-
<p>Please go
55+
<p>Please go
5656
<a href="https://github.com/typicode/jsonplaceholder/issues">here</a>.
5757
</p>
5858

src/cli.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)