json-server
json-server copied to clipboard
Doesn't update on changes to DB?
Is it by design or a bug, that the response does not update on changes? Do I have to manually restart the server every time I make changes to a .json file served?
did you try passing --watch
option as described here ?
Doesn't work with ---watch
enabled for me either, version 0.12.1
. I start my server:
json-server --watch db.json
, then I modify db.json
and reload the server endpoint (GET) - it still holds the same data as if the JSON file was unmodified
@adriangonciarz same for me to with 0.12.1, do you have found a solution ? :)
not exactly the same issue, but I was wondering if anyone found a way to have live updates working when running on node js as a module?
I am having the same issue with 0.12.1, watch is not recognizing updates to the json file
+1
I had same issue and have done some tests on mac, raspi and a window 7 machine. Maybe it fix your problems as well. Had done some tests and was surprised that the file was loaded but not visible at the filesystem (i.e. json-server --watch test123.json). The api works, but no file output at the windows machine. For mac and raspi i found the entries at the user folder. Summary: a) the mac and raspi store the db.json and snapshots in the user roor (~/) directory , so if you place your file to watch there it works => after an update and save the new data are available, same for the posted via browser b) windows seems to have a problem with linux like file system, therefore use the complete file path, for example C:\Users\User\db.json and it works with update in file and via browser
Hey guys, @bollitec seemed to be having the correct answer. You need to pass the complete full path in the --watch flag in order for it to work in windows. Hope the developer fix this major bug, it is quite difficult to spot here.
It still exists, any updates on this? passing --watch along with server start is also not working
This is my workaround on macOS. It requires fswatch
(brew install fswatch
).
fswatch -o schema.js | xargs -n1 -I{} touch -am routes.json
Since watch does work for my config.json
and my routes.json
, I'm telling fswatch to watch my DB file, schema.js
. Any time I save that file, touch -am routes.json
updates the access and modified timestamps on routes.json
.
+1 when I update PATCH the data, the .json
file shows the right data, but json-server
seems to be serving old data, like it was cached or something. If I get rid of --watch
it seems to work correctly.
Seems to be related to #177
I am having the same problem too... My start script in package is: "start": "node server.js --watch db.json --port 3004",
If i make any delete an item in an array from "db.json" and make a GET request, it is returning the old contents. The only way I can get this to work is if i manually catch the request by middleware:
server.get('/cats', (req, res, next) => { if (req.method === 'GET') { res.status(200).jsonp(db.cats); } });
This defeats the point of json-server though, might as well just use plain express if I am going to setup a route for every single case...
I am having the same issue as the deleted user (https://github.com/typicode/json-server/issues/710#issuecomment-366005912).
I need to use JSON Server as a module but when I create a new record the db.json
content is updated but the endpoint does not return the updated values when it is called again.
I was facing the same issue in Linux 18.04 but after adding --watch and complete file path along with the different port it seems to be working fine. Even the server restart is not required.
Hi, my test json updates after 5 minutes, is this intentional?
use nodemon
👍
start.js
:
// github.com/remy/nodemon
var nodemon = require('nodemon');
nodemon({
script: 'index.js',
ext: 'js json' // watching extensions
});
nodemon.on('start', function () {
console.log('App has started');
}).on('quit', function () {
console.log('App has quit');
process.exit();
}).on('restart', function (files) {
console.log('App restarted due to: ', files);
});
index.js
:
const jsonServer = require('json-server')
const data = require('./api/db.js')
const routes = require('./api/routes.json')
const server = jsonServer.create()
const router = jsonServer.router(data)
const middlewares = jsonServer.defaults()
// github.com/typicode/json-server/issues/690#issuecomment-348616467
// json-server options.bodyParser defalut is true
// server.use(jsonServer.bodyParser);
server.use(middlewares)
server.use(jsonServer.rewriter(routes))
server.use(router)
// Avoid CORS issue
// json-server options.noCors defalut is false
// server.use( (req, res, next) => {
// res.header("Access-Control-Allow-Origin", "*");
// // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });
server.listen(9538, () => {
console.log('JSON Server is running, see http://localhost:9538')
})
All you need to do is exec the command node start.js
to watching to all files
Just encountered this issue. Scanned the docs and found out that you have to explicitly set the Content-Type
for PATCH
, etc.
headers: {
'Content-Type': 'application/json'
},