Json-server: Disabling Changes

Created on 10 May 2015  路  9Comments  路  Source: typicode/json-server

Are there options for:

  • Disabling the POST/PUT/PATCH/DEL requests?
  • Disabling changes to the JSON file.

If these are not presently available, it would be greatly beneficial if they were added, as that way json-server would also be able to serve as a mechanism for delivering read-only JS databases with the ability to be sorted and searched.

Most helpful comment

It depends how much "read-only" you want to achieve.
But I would really be interested in knowing the use-case :)

To disable changes to JSON file, you can use a JS file:

// db.js
module.exports = function () {
  return { posts: [ ... ] }
})
json-server db.js # it will be an in-memory database and nothing will be written to disk

If you want to disable/modify specific routes, it's better to use json-server as a module:

var jsonServer = require('json-server')

var server = jsonServer.create()

// Filter by req.method
server.all('*', function (req, res, next) {
  if (req.method === 'GET') {
    next() // Continue
  } else {
    res.sendStatus(403) // Forbidden
  }
})

server.use(jsonServer.defaults)

// You could also use an object here instead of a file
server.use(jsonServer.router('db.json')) 

server.listen(3000)

I've not tested this code, but it should work. If it doesn't let me know.

You can have a look as well at:
https://github.com/typicode/jsonplaceholder/blob/master/index.js

It restores database on each requests. So it's kind of read-only too.

Hope this will help :)

All 9 comments

sure need help, is this me?

@typicode?

It depends how much "read-only" you want to achieve.
But I would really be interested in knowing the use-case :)

To disable changes to JSON file, you can use a JS file:

// db.js
module.exports = function () {
  return { posts: [ ... ] }
})
json-server db.js # it will be an in-memory database and nothing will be written to disk

If you want to disable/modify specific routes, it's better to use json-server as a module:

var jsonServer = require('json-server')

var server = jsonServer.create()

// Filter by req.method
server.all('*', function (req, res, next) {
  if (req.method === 'GET') {
    next() // Continue
  } else {
    res.sendStatus(403) // Forbidden
  }
})

server.use(jsonServer.defaults)

// You could also use an object here instead of a file
server.use(jsonServer.router('db.json')) 

server.listen(3000)

I've not tested this code, but it should work. If it doesn't let me know.

You can have a look as well at:
https://github.com/typicode/jsonplaceholder/blob/master/index.js

It restores database on each requests. So it's kind of read-only too.

Hope this will help :)

Thanks! Should I make a pull request to add these as options, so that the user can choose for these options to be enabled?

No thanks. For the moment, I prefer the CLI to stay simple and let people customize what they want using the project as a module.
Also, I think it would be hard to cover all the particular cases using CLI options.
But, if it happens to be a common case to restrict HTTP verbs, I'll add it to the CLI.

BTW, why did you need to only have GET?

A potential use case I have concerning transforming very big dataset into an API, reducing the required amount of server side code to be written.

To disable changes to JSON file, you can use a JS file ...
json-server db.js # it will be an in-memory database and nothing will be written to disk

You don't know how happy I was to learn about this little nugget of a tip - using js files instead of json for an in-memory solution. Thanks for the productivity boost you just gave me. I'll try and make a PR to add this to the project's Readme.

how i do to persist modification in this case
json-server file.js # won't write

Is there a way to disable changes to the object itself? So that way for a given route, it will always return the same data no matter what I post or put into it? I'm using the JS way. But I don't want to just disable POST/PUT... etc, I want it to return the same data no matter what I PUT.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pantchox picture pantchox  路  3Comments

melnikovic picture melnikovic  路  3Comments

goldmont picture goldmont  路  3Comments

Isanderthul picture Isanderthul  路  3Comments

strom picture strom  路  4Comments