Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Ahmed Ayoub

    or if you want more generic way of handling your static assets, you could use --static option in the command line

    create public/assets/index.html file create directories if not exist

    json-server db.json --static public

    will serve public/static/index.html .. and so on

    Per Wiklander

    I have models with optional relations to eachother and have a problem with DELETE.

    The data for a model foo could look like this initially:

      "foos": [
          "id": 1,
          "title": "A foo"

    After adding a bar and connecting it to my foo it would look like this:

      "foos": [
          "id": 1
          "title": "A foo",
          "barId": 1
      "bars": [
          "id": 1
          "title": "A bar"

    Now I want to DELETE the foo with id 1. But I don't want to delete the bar. Speaking in terms of SQL I do not want an ON DELETE CASCADE as it looks like it is doning now. In case I DELETE the bar instead I would like the barId property in the foo to either be set to undefined (like with ON DELETE SET NULL) or just removed from the foo object.

    Is this possible with json-server today?

    Ahmed Ayoub

    @PerWiklander short answer. No.

    Long answer:
    json-server is an express server so you can do all kind of workarounds you wish.

    You should first use json-server as module not CLI.

    Giving that json-server db is in json-server router object

    One workaround on my mind now is to write a simple middleware that intercept DELETE /foos/:fooIDthen using lowdb's API, get thebar by its id, update the value, then save the db db.write()

    Please find my anser here, might help too

    please let me know if this is not clear so I can find sometime to create an example for you.

    Per Wiklander
    Thanks, I’ll have a look. I guess this is getting close to where the actual server is easier to use than the json mock.
    Ahmed Ayoub
    @PerWiklander not always, most of the time json-server is so useful, its out-of-the-box is more than enough for mocking (not for complex business logic)
    Per Wiklander
    yeah, it has worked well until this problem showed up. I guess I'll just have to create custom routes for the models that do not want to be handled in the ordinary "clean all orphans on delete" way.
    Ahmed Ayoub
    @PerWiklander :+1:
    Félix Pelletier
    is this project still maintained?
    it seems that the issues are piling up without any answers :(
    Hi @felpel, yes, it's still maintained. Even though I'd love too, I don't have the time to work on all projects and be able to answer to everyone unfortunatly (BTW I'm very thankful to people helping beginners in issues! <3).
    Also to be able to make significant progress on each projets, I'm rotating between them: hotel got many updates a few months ago, now I'm focusing on husky first v1.0 release and next will probably be JSON Server.
    To improve this situation, I'm looking at ways to make development more sustainable (Patreon, Open Collective, Sponsoring, other ideas, ...).
    Arye Shalev
    @typicode thank you so much for JSON server! it saved my life so many times! but then when migrating to a real database, i find it hard to configure all the routes that JSON-server did them automaticlly just by reading the JSON file! do you think there is a way to maybe generate express api endpoints based on JSON file schema as you did for SQL or mongo?
    @typicode How do I set a custom route with Post requests enabled?
    good project
    Pavels Ahmadulins
    @typicode I'm looking for a way to modify (wrap) my responses. I've found examples with .render method but in TypeScript with @types/json-server this method does not exist. What I need to do is convert body of response from object[] to { data: object[], totalCount: header.get('X-Total-Count') }.
    moritz bust
    How do I create randomly generated content in a modularized setup with custom rotes?
    I am trying to follow the examples, but there is no real link between the index.js example and the server.js example
    I would like to use url parameters when generating random content.
    Wilgert Velinga
    could this PR typicode/json-server#931 be merged

    could this PR typicode/json-server#931 be merged

    Could this be merged please?

    Sourodeep Chatterjee
    Hello everyone, I want to create my JSON server more intelligent with dynamic routing. It can do pagination, filter based on the object of POST request etc. I know that express JS is a solution, but I want to know whether there is any other solution, where I can do all these things on my own, without express js
    middleware on public routes? is it possible?
    How do I configure json-server to send response with transfer-encoding:chunked corresponding to accept-encoding:application/json ?
    Oscar Lodriguez


    Anyone know a workaround for de DELETE method on ID?

    There is a long standing issue that will most likely not get resolved. typicode/json-server#756
    Stephen Dougherty
    Is there anyway with json-server to use custom id's for different routes. I am aware of the cli option but that seems to be a global thing. Our models unfortunately have been set up with different id's and we would like to keep them as is at this stage.