Json-server: Parsing multiple querystring params

Created on 14 Aug 2017  路  5Comments  路  Source: typicode/json-server

We're using json-server as a module and trying to parse multiple query string parameters. All of the features we need seem to already exist (Paginate, Sort, Slice, Full Text Search, etc.), and individually we can successfully write custom routes to use one feature at a time (re-write our search URL to what makes json-server happy ), but in trying to write complex custom routes that handle multiple query string parameters, json-server drops everything after the first query string.

For example, this URL:

/users/search?pageIndex=0&pageSize=20&q=&sortOrder=asc&status=ACTIVE

always appears as:

/users/search?pageIndex=0

according to json-server.

I've been through a lot of the json-server code, and I'm trying to figure out where I can intercept the request before it gets cut short, and even tried writing a mixin that intercepted the request before json-server's router was invoked, but couldn't surface the original URL anywhere, no matter what we do it gets shrunk down.

It appears some other users have tried to build similar functionality, with no success:
https://github.com/typicode/json-server/issues/468 - user trying to mimic elasticsearch
https://github.com/typicode/json-server/issues/96 - in this thread typicode says it doesn't seem possible to do a deep query in this way
https://github.com/typicode/json-server/issues/318 - user trying to use 3 query string params

We have found json-server a pleasure to use, and have implemented it as a module with some customized functionality, including custom routing, custom error handling, re-mapping some features to fit our specific use-cases, and a few other features.

If you could point us in the right direction as to how json-server could parse multiple parameters, we would be happy to consider the necessary effort and contributing it as a PR.

If there is a way to do this now, we would be incredibly excited of course!

Thanks!

All 5 comments

Hi @mangoceylon, did you find a way around this? We're running into the same issue. Thanks!

@mangoceylon @juancarlosfarah This is maybe not ideal, since you'd be re-parsing the query string, but I found the original (at least for GET requests) was still hidden away inside req, so this worked for me:

//...
const querystring = require('querystring');

server.use(middlewares);
router.render = (req, res) => {
  const parsed_query = querystring.parse(req._parsedUrl.query);
  // after that use parsed_query.whatever for ?q=a&whatever=b

querystring comes with Node.js, so no need to add anything to package.json.

Any updates on this?? did anyone find a way around this? We're running into the same issue. Thanks!

@riyazmulla51 Did you try my workaround just above your comment?

@codingthat Thank you for your quick response. And for you solution too. Yes it is working (tried for GET request). Previously i was trying something like,

//server.js

const errorHandler = require("./errorHandler.js");

const querystring = require("querystring");
router.render = (req, res) => {
let parsed_query = querystring.parse(req._parsedUrl.query);
errorHandler(req, res, parsed_query);
};

//errorHandler.js is a new file i created to write logic so that server.js contains less code lines
//errorHandler.js

const errorHandler = (req, res, queryparameters) => {
console.log(queryparameters);
});

//when i hit the request sometimes i get null object and sometimes it works as expected, like query params will console logged in above scenario.

//output: sometimes getting null object
[Object: null prototype] { }

Apart from this, if we used in the same server.js file it'll work as expected.
Thank you @codingthat for your solution. :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Isanderthul picture Isanderthul  路  3Comments

sboudouk picture sboudouk  路  3Comments

AdamCook44 picture AdamCook44  路  3Comments

melnikovic picture melnikovic  路  3Comments

jasonlimantoro picture jasonlimantoro  路  4Comments