Comments (8)
Yes.
Something like that should work:
var express = require('express');
var jsonServer = require('json-server')
var db = {
posts: [
{ id: 1, body: 'foo' }
]
}
var server = express()
// custom routes
server.post('/login', function(req, res) {
res.json({name: "John Doe", access_token:"3A364F68DA5745EB653457A585678"});
})
// mount jsonServer
server.use(jsonServer(db))
server.listen(3000)
It comes from this thread #30
Does it solve your problem?
from json-server.
Yes, this solves my problem.
However, I have further motivation that is unaddressed by custom routing, on which I invite your comment. json-server is so handy for use as a local test/dev server, but I can't figure out how to "refresh" the server from the initial dataset, as needed in testing. Any comment on doing this with json-server (or anything else) much appreciated.
G
from json-server.
Cool.
Not sure to understand what you mean by "refresh"? Could you give me an example?
from json-server.
Yes, thanks.
My problem is likely mundane, but obscure, I guess, and I have not yet
found a solution from other sources.
I am using json-server to represent a "real" REST service for integration
testing of javascript clients, e.g., an Angular app. I need to refresh the
test data in my json-server after certain automated tests. So, I would add
a custom route to my json-server, like as follows:
...
var expressServer = express();
expressServer.post('/refresh-data', function(req, res) {
// refresh the json-server: access the lowDB instance and read back in
the initial data file
});
expressServer.use(server(obj)).listen(3000);
...
On Mon, Nov 10, 2014 at 2:51 AM, typicode [email protected] wrote:
Cool.
Not sure to understand what you mean by "refresh"? Could you give me an
example?—
Reply to this email directly or view it on GitHub
#33 (comment).
from json-server.
Ok I see.
It's something missing in the project API and I should add it.
I'll try to do it this week.
from json-server.
Actually, using lodash
, you could do this:
var _ = require('lodash')
var db = {
// ...
}
var original = _.cloneDeep(db)
function reset() {
// Empty db object
_.each(db, function(value, key) {
delete db[key]
})
// Copy original into db
_.extend(db, _.cloneDeep(original))
}
jsonServer(db)
from json-server.
Your reset() is shown to work in my 1-2 below, but the server does not
reflect the change for any subsequent GET request...
expressServer.post('/refresh-data', function(req, res) {
console.log("data before reset: " + JSON.stringify(obj)) // 1
reset();
console.log("data after reset: " + JSON.stringify(obj)) // 2
res.end();
});
expressServer.use(server(obj)).listen(3000);
G
On Mon, Nov 10, 2014 at 1:56 PM, typicode [email protected] wrote:
Actually, using lodash, you could do this:
var _ = require('lodash')
var db = {
// ...
}
var original = _.cloneDeep(db)
function reset() {// Empty db object
_.each(db, function(value, key) {
delete db[key]
})// Copy original into db
_.extend(db, _.cloneDeep(original))}
jsonServer(db)
—
Reply to this email directly or view it on GitHub
#33 (comment).
from json-server.
I am trying to use this for angular app is there any example that I can use. I am new to this.
from json-server.
Related Issues (20)
- How do I encrypt json-server HTTP requests? HOT 1
- problem in pagination HOT 1
- Why doesn't the "name_like" parameter work in the latest version of the JSON server? HOT 1
- error/bug in the json server showing INT value as a STRING HOT 2
- Running "json-server --watch ./data/db.json --port 8000 --routes ./data/routes.json" results in the error "Unknown option '--routes'" HOT 5
- Issues with installing to non alpha version HOT 3
- How do I set up a unique road handler? HOT 1
- Error on : json-server --watch db.json HOT 1
- query parameter does not working HOT 3
- Weird behaviour when property "period" is in json mock data
- Steps to serve multiple json files with json-server HOT 3
- `_gte` and `_lte` doesn't work for dates HOT 3
- In new version not working `_sort=price&_order=asc` , please fix this issue.
- Multiple filters not working
- Whats cuestión? For the api?
- cors problem HOT 2
- Has the --delay option been removed? HOT 5
- not working at all
- conditional operators doesn't work
- I have a problem opening Json-server --watch.db.json HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-server.