json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Doesn't update on changes to DB?

Open blaasvaer opened this issue 6 years ago • 17 comments

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?

blaasvaer avatar Jan 09 '18 13:01 blaasvaer

did you try passing --watch option as described here ?

Nilegfx avatar Jan 16 '18 02:01 Nilegfx

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 avatar Jan 24 '18 21:01 adriangonciarz

@adriangonciarz same for me to with 0.12.1, do you have found a solution ? :)

kmoreauSilab avatar Feb 12 '18 15:02 kmoreauSilab

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?

ghost avatar Feb 15 '18 17:02 ghost

I am having the same issue with 0.12.1, watch is not recognizing updates to the json file

jseelna avatar Feb 18 '18 20:02 jseelna

+1

CyberNika avatar Mar 14 '18 16:03 CyberNika

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

bollitec avatar Apr 01 '18 11:04 bollitec

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.

jiang199x avatar Apr 23 '18 02:04 jiang199x

It still exists, any updates on this? passing --watch along with server start is also not working

hansiemithun avatar Aug 28 '18 12:08 hansiemithun

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.

natchiketa avatar Nov 21 '18 18:11 natchiketa

+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

quangv avatar Sep 26 '19 19:09 quangv

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...

rezelute avatar Oct 15 '19 17:10 rezelute

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.

vcpablo avatar Nov 06 '19 19:11 vcpablo

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.

SonaliSihra avatar May 04 '20 14:05 SonaliSihra

Hi, my test json updates after 5 minutes, is this intentional?

dgloriaweb avatar Jun 03 '20 01:06 dgloriaweb

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

PLQin avatar Sep 09 '20 12:09 PLQin

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'
},

calvo-jp avatar Feb 22 '22 08:02 calvo-jp