Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Michele Gatti
    @mikeleg
    Is possible to configurate json-server with a docker container?
    Kavan72
    @Kavan72
    is it possible to this project work with sqlite database not in json file
    Marcus Kovács
    @garcus
    I'm strugling to figure out if there is a way in a server.get() function to look at data within the watched json database file ?
    I thought, maybe use the router object but I cant find a way to access the data. I like to validate something in a server.post('/someurl'...) before I send the response
    Arye Shalev
    @pantchox
    Love this server!
    question related to lowdb, how can i use chained functions with iterating such as map/foreach/mapValues etc, it seems it can't be since it s a collection.
    i have this issue because i have nested objects and arrays.
    Arye Shalev
    @pantchox

    for example i have this json

    [
      {
        shelveId: 0,
        books: [{...},{...},{...}]
      },
      {
        shelvId: 1,
        books: [{...},{...}]
      }
    ]

    i would like to get a json as {shelveId, numBooks} so in our case:

    {
      "0":  3,
      "1": 2
    }

    how can i acheive that with the server using the lowdb functions and lodash i tried but no example of foreach or map, thanks!

    Tameshwar Nirmalkar
    @TameshwarNirmalkar
    how to achieve pagination in custom routes in json-server

    "/test/:start/:end": "/pagination?_start=:start&_end=:end"

    this doesn't work

    can anybody help me on this
    Tameshwar Nirmalkar
    @TameshwarNirmalkar
    "/test/:start/:end": "/list?_start=:start&_end=:end"
    thijs
    @thijskoek
    Hey guys, what does --watch do?
    Darktripe
    @Darktripe
    Hi everyone. I am using json-server to do a version of a portal in standalone. The portal does a request with a pdf response. Does anyone know how to give a pdf in response ? I have the pdf in local folder
    Jon Duffy
    @JonDuffy
    @thijskoek pass a json file to --watch and json sever will serve it
    I can't quite work out how to do deeper routes . for example /rockets/launches/{1}
    Jon Duffy
    @JonDuffy
    or maybe, how to serve nested data
    would be a better description
    Darktripe
    @Darktripe
    One more question. I have a db.json and a routes.json : routes.json reference all routes of my json-server but I have an error when i start my json-server : Range out of order in character class at Regexp (native). My route is this : "/myroutes/reports[001]"
    I know that this character "[" and this "]" caused the error but i can't remove this characters rom the request
    Can anyone help me please ?
    masaanli
    @masaanli
    Hi how can i make it possible to access /text/get for example?
    masaanli
    @masaanli
    json server Oops, found / character in database property can i make a / work?
    Victor Guilherme Santos Hogemann
    @vhogemann
    hi folks, how can I access the db from a custom route? Or this is only possible from the router.render() method?
    Arye Shalev
    @pantchox
    Hi guys, i love this json-server it saved my life! i wonder if anyone here found a semi automated solution like this with real DB? that create auto routes for _embed and _expand by a structure of you JSON file with real DB such as Mongo or Sqlite? thanks!
    André Drougge
    @miphe
    Hi all
    Great mock server, working great for me developing my front-end while the API guys are too slow.
    I want to specify a response from the server, when I do a POST. I have a middleware but I can’t really see where I can change the response body in the res.
    André Drougge
    @miphe
    I see there’s an idea about authentication in the docs, however I still have the same issue.
    in my isAuthenticated(req) method, I want to check if the request had a certain header (auth token). I keep doing console.log(req) to see where I can find the headers I send.. but to no avail - I can not for the life in me find headers in the request.
    req.headers doesn’t show the token that I do send in my req, actually those headers are all different from the ones I send.
    André Drougge
    @miphe
    Don’t really want to mention, but I think this is such a simple thing if you know how to reach it - @typicode perhaps you can shed some light on this.
    André Drougge
    @miphe
    André Drougge
    @miphe
    Perhaps @hlobil or @imikay ?
    Arye Shalev
    @pantchox
    @miphe you answered yourself(!)
    req.headers is the object of the headers sent from the client
    const isAuthorized = (req) => {
      return req.headers['AUTH_TOKEN'] ? true : false;
    }
    in your question you pasted the request headers of your client and it DOESNT send 'auth_token`
    just add it to your request headers of any AJAX lib you are using
    Jafeth Díaz
    @jafethtk
    Hi, how would I set custom ids for different resources?
    oliverkk
    @OliverKK
    Hi guys,
    does anybody know how to add simple headers to a specifgied request?
    oliverkk
    @OliverKK

    I have a nice solution using --middleware ;)

    package.json:
    "startAPI": "json-server --watch server/mockedBackendAPI.json --routes server/mockedBackendRoutes.json --port 3004 --middlewares server/middleware.js"

    middleware.js:
    module.exports = (req, res, next) => { res.header('X-Hello', 'World') next() }

    rajneeshmandiwal
    @rajneeshmandiwal
    Hi guys,
    I want to know how to store uber acc in another country uber server.
    Nikos
    @QuantumInformation
    Anyone got a quick solution to this? typicode/json-server#700
    OY
    @TW-OY
    Anyone has any idea that how can mock return an html?
    e.g.
    I want to visit localhost:3001/test to actually visit my public/assets/index.html
    Ahmed Ayoub
    @Nilegfx

    @TW-OY
    save this following code in a file called middlewares

    module.exports = (req, res, next) => {
        console.log('url', req.url);
        console.log('path', req.path);
        if (req.url == '/test') {
            res.redirect('/');
        }else{
            next();
        }
    };

    then run json-server as follow:

    json-server db.json --middlewares middlewares.js

    browse:
    http://localhost:3000/test

    Ahmed Ayoub
    @Nilegfx

    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

    http://localhost:3000/assets
    will serve public/static/index.html .. and so on

    Per Wiklander
    @PerWiklander

    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
    @Nilegfx

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