json-server
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -6,2 +6,5 @@ #!/usr/bin/env node | ||
logger = require('../utils/logger'), | ||
moment = require('moment'), | ||
fs = require('fs'), | ||
pkg = require('../package.json'), | ||
options = {}; | ||
@@ -32,17 +35,50 @@ | ||
function saveDbOnCommand(app) { | ||
console.assert(app, 'expected app object'); | ||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
logger.notice('To save live database at any moment, enter `s`'); | ||
process.stdin.on('data', function (userInput) { | ||
if (userInput.trim().toLowerCase() == 's') { | ||
var liveDB = app.db(); | ||
var now = moment().format('YYYY-MM-DD-HH:mm:ss') | ||
var filename = 'json-server.' + now + '.json'; | ||
console.assert(liveDB, 'expected live db object'); | ||
fs.writeFileSync(filename, | ||
JSON.stringify(liveDB, null, 2), | ||
'utf-8'); | ||
console.log('saved db to', filename); | ||
} | ||
}); | ||
} | ||
function onDatabaseLoaded(db) { | ||
server.run(db, options); | ||
var app = server.run(db, options); | ||
saveDbOnCommand(app); | ||
return app; | ||
} | ||
program | ||
.version('0.1.0') | ||
.version(pkg.version) | ||
.option('-f --file <file>', 'load db from a js or json file') | ||
.option('-u --url <url>', 'load db from a URL') | ||
.option('-p --port [port]', 'server port') | ||
.option('--read-only', 'read only mode') | ||
.parse(process.argv); | ||
.option('--read-only', 'read only mode'); | ||
program.on('--help', function () { | ||
var examples = | ||
' Examples:\n\n' + | ||
' json-server --file db.json\n' + | ||
' json-server --file seed.js\n' + | ||
' json-server --url http://example.com/db.json\n' | ||
console.log(examples); | ||
}); | ||
program.parse(process.argv); | ||
if (program.port) options.port = program.port; | ||
if (program.readOnly) options.readOnly = true; | ||
if (program.file) loadFile(program.file, onDatabaseLoaded); | ||
if (program.url) loadURL(program.url, onDatabaseLoaded); | ||
if (program.url) loadURL(program.url, onDatabaseLoaded); |
{ | ||
"name": "json-server", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Serves JSON files through REST routes.", | ||
@@ -17,3 +17,4 @@ "main": "server.js", | ||
"underscore": "~1.5.2", | ||
"underscore.inflections": "~0.2.1" | ||
"underscore.inflections": "~0.2.1", | ||
"moment": "~2.4.0" | ||
}, | ||
@@ -20,0 +21,0 @@ "devDependencies": { |
@@ -19,4 +19,4 @@ [![Build Status](https://travis-ci.org/typicode/json-server.png)](https://travis-ci.org/typicode/json-server) | ||
{ | ||
posts: [ | ||
{ id: 1, body: 'foo' } | ||
"posts": [ | ||
{ "id": 1, "body": "foo" } | ||
] | ||
@@ -28,2 +28,5 @@ } | ||
You can type 's' at any moment to save the current live JSON object to timestamped | ||
file. | ||
### Node module | ||
@@ -34,5 +37,5 @@ | ||
var db = { | ||
var db = { | ||
posts: [ | ||
{id: 1, body: 'foo'} | ||
{ id: 1, body: 'foo' } | ||
] | ||
@@ -58,3 +61,3 @@ } | ||
```bash | ||
$ npm install -g json-server | ||
$ npm install -g json-server | ||
``` | ||
@@ -104,9 +107,9 @@ | ||
{ | ||
posts: [ | ||
{ id: 1, body: 'foo'}, | ||
{ id: 2, body: 'bar'} | ||
"posts": [ | ||
{ "id": 1, "body": "foo" }, | ||
{ "id": 2, "body": "bar" } | ||
], | ||
comments: [ | ||
{ id: 1, body: 'baz', postId: 1} | ||
{ id: 2, body: 'qux', postId: 2} | ||
"comments": [ | ||
{ "id": 1, "body": "baz", "postId": 1 } | ||
{ "id": 2, "body": "qux", "postId": 2 } | ||
] | ||
@@ -121,5 +124,5 @@ } | ||
var data = {}; | ||
data.posts = []; | ||
data.posts.push({id: 1, body: 'foo'}); | ||
data.posts.push({ id: 1, body: 'foo' }); | ||
//... | ||
@@ -189,4 +192,4 @@ | ||
```bash | ||
$ npm install | ||
$ npm install | ||
$ npm test | ||
``` |
@@ -15,2 +15,6 @@ var _ = require('underscore'), | ||
exports.db = function() { | ||
return db; | ||
} | ||
// GET /:resource?attr=&attr= | ||
@@ -26,3 +30,3 @@ exports.list = function(req, res) { | ||
}); | ||
if (_(properties).isEmpty()) { | ||
@@ -41,9 +45,9 @@ result = collection; | ||
resource; | ||
// Set parentID | ||
properties[req.params.parent.slice(0, - 1) + 'Id'] = +req.params.parentId; | ||
// Filter using parentID | ||
resource = _.where(db[req.params.resource], properties); | ||
res.jsonp(resource); | ||
@@ -55,3 +59,3 @@ } | ||
var resource = _.get(db, req.params.resource, +req.params.id); | ||
res.jsonp(resource); | ||
@@ -58,0 +62,0 @@ } |
@@ -50,3 +50,3 @@ var express = require('express'), | ||
app.options('*', cors()); | ||
// Set app.router | ||
@@ -67,2 +67,3 @@ app.use(app.router); | ||
routes.setDatabase(db); | ||
app.db = routes.db; | ||
@@ -74,3 +75,3 @@ // And done! Ready to serve JSON! | ||
function run(db, options) { | ||
options = _.defaults(options, defaultOptions); | ||
options = _.defaults(options || {}, defaultOptions); | ||
@@ -94,5 +95,6 @@ var app = createApp(db, options); | ||
}); | ||
return app; | ||
} | ||
exports.createApp = createApp; | ||
exports.run = run; | ||
exports.run = run; |
@@ -7,2 +7,3 @@ var logan = require('logan'); | ||
info: ['%', 'grey'], | ||
notice: ['%', 'yellow'], | ||
url: [' http://localhost:%/'.grey + '%'.cyan, '.'] | ||
@@ -9,0 +10,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30016
666
189
8
6
+ Addedmoment@~2.4.0
+ Addedmoment@2.4.0(transitive)