kitloong/json-server-vercel

POST & DELETE request 500

nicopcat opened this issue ยท 7 comments

Try to make post request but fail:

Error: EROFS: read-only file system, open './db.json'
at Object.openSync (node:fs:601:3)
at writeFileSync (node:fs:2249:35)
at FileSync.write (/var/task/node_modules/.pnpm/lowdb@1.0.0/node_modules/lowdb/adapters/FileSync.js:53:14)
at db.write (/var/task/node_modules/.pnpm/lowdb@1.0.0/node_modules/lowdb/lib/main.js:37:21)
at /var/task/node_modules/.pnpm/json-server@0.17.0/node_modules/json-server/lib/server/router/write.js:5:8
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/express@4.18.1/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/express@4.18.1/node_modules/express/lib/router/route.js:144:13)
at create (/var/task/node_modules/.pnpm/json-server@0.17.0/node_modules/json-server/lib/server/router/plural.js:246:5)
at Layer.handle [as handle_request] (/var/task/node_modules/.pnpm/express@4.18.1/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/.pnpm/express@4.18.1/node_modules/express/lib/router/route.js:144:13)

I fixed this by adding these three lines at the top of the file /api/server.js.

const fs = require("fs")
const path = require("path")
const db = JSON.parse(fs.readFileSync(path.join("db.json")))

Then you can update router to use the db const.

const router = jsonServer.router(db)

Thank you, your code is working well.

@nicopcat , @VicAv99 thanks for covering this issues on regarding server deployment. My another question, is that you guys have an experience using json-server as I know from that issue so , my mind is thinking that - - when a post, put or update req happens then the data is modified ? & then if data is modified then the db.json also updated right? is this actually works here or not? I've found many post that data is not save in db.json so data will gone when the server stop or after execution . . Can you guide me to that . . Thank you again both of you

Thanks @VicAv99 , I have added your solution to the ./api/server.js as an option.

Thanks @VicAv99 , I have added your solution to the ./api/server.js as an option.

this is so nice - appreciate everyone.

I am confused! Are you guys implementing this for persisting data till the serverless function dies? The data will just get deleted after the serverless function stops. If it works like that why is it disabled/commented out? Even in the demo app, you can enable POST/DELETE without any consequence because data will get deleted!

Am I missing something?

I was having great difficulties, so thank you very much!!