typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.66k stars 7.01k forks source link

Doesn't update on changes to DB? #710

Open blaasvaer opened 6 years ago

blaasvaer commented 6 years ago

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?

Nilegfx commented 6 years ago

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

adriangonciarz commented 6 years ago

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

kmoreauSilab commented 6 years ago

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

ghost commented 6 years ago

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?

jseelna commented 6 years ago

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

CyberNika commented 6 years ago

+1

bollitec commented 6 years ago

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

jiang199x commented 6 years ago

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.

hansiemithun commented 6 years ago

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

natchiketa commented 5 years ago

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.

quangv commented 5 years ago

+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

rezelute commented 4 years ago

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

vcpablo commented 4 years ago

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.

SonaliSihra commented 4 years ago

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.

dgloriaweb commented 4 years ago

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

PLQin commented 4 years ago

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

calvo-jp commented 2 years ago

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'
},
PuzzleDotsCode commented 1 month ago

Hack fix: after wasting hours of life that never gonna be back, use the mothaf*kr Nodemon, check this:

    "dev:json-server": "nodemon --watch db.json --exec \"json-server db.json --port 3001\"",
    "dev": "concurrently \"npm run dev:next\" \"npm run dev:json-server\"",
    "dev:next": "next dev",