json-server
json-server copied to clipboard
--watch does not work when running with docker-compose
I need to regenerate random data in each GET request. Since the data generation script can only be executed once during the json-server startup, I added a middleware function which opens the static data file, modifies partial data and write back to the same file. When I start up json-server on VM, the watch command seems to work and I get different data every time. However, the watch command does not seem to work when running in Docker container (specifically in docker-compose.yml) and I get the same data even the file is modified. Looks like the watch does not detect the file change and data is not reloaded. Can someone please assist?
Instead of this solution, is there a better solution? For example, is it possible to rerun data generation script after each GET request?
json-server: image: clue/json-server command: -w db.json -middlewares ./update.js -host 0.0.0.0 volumes:
- ./db.json:/data/db.json
- ./update.js:/data/update.js
I'm not sure if this is related, but it also doesn't work for me in the other direction. It reads from the db.json file, but then never updates it again.
I noticed that json-server depends on a lowdb version from 2 years ago (^0.15.0). However, the json-server code on master uses (^1.0.0).
https://github.com/typicode/json-server/commit/fda4e3e722b12071caeae1f5c68f7e422eaf76bf#diff-b9cfc7f2cdf78a7f4b91a753d10865a2
I wonder if this is supposed to fix that issue. I also tried using master, but that doesn't work at all. I get an error about the json-server module not being found. I'm giving up.
Is there a workaround/alternative to get this working? Did the same thing (Using it inside a container) but doesn't pick up the file changes
Not that I know of. My "workaround" was to stop using this library.
unfortunately it looks like fs.watch doesn't play well with docker volumes, it should be replaced with gaze in order to make it work
im also having this issue the db is sitting on a cephfs mount on a kubernetes pod and updates to db.json is not reloading the server seems it has been this way for a while now #537
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() // Express server
const router = jsonServer.router(data) // Express router
const middlewares = jsonServer.defaults()
// https://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 listen to all files
The workaround is to specify directories at volumes: , not files.
Here is the code that worked.
services:
json-server:
image: clue/json-server
command: --watch /data/db.json --host 0.0.0.0
ports:
- "8880:80"
volumes:
- ./data:/data
The workaround is to specify directories at
volumes:, not files.Here is the code that worked.
services: json-server: image: clue/json-server command: --watch /data/db.json --host 0.0.0.0 ports: - "8880:80" volumes: - ./data:/data
That still doesn't work for me on Windows 11, Docker version 20.10.24, build 297e128, JSON Server 0.17.4. I am using Docker Compose.