Njs: querystring module.

Created on 11 Feb 2020  路  7Comments  路  Source: nginx/njs

Hi, @xeioex @drsm

Help to check if it's a good idea to support getting the post body with object value.
Like this. https://github.com/openresty/lua-nginx-module/blob/master/t/031-post-args.t
Actually, I need such a way in njs. Welcome to your suggestion.
It's also OK to parse it by js code.

feature

Most helpful comment

@hongzhidao
Hi!

Actually, I need such a way in njs. Welcome to your suggestion.

I think it would be nice to have a native querystring module in njs.

For reference: https://github.com/nodejs/node/blob/master/lib/querystring.js

All 7 comments

@hongzhidao
Hi!

Actually, I need such a way in njs. Welcome to your suggestion.

I think it would be nice to have a native querystring module in njs.

For reference: https://github.com/nodejs/node/blob/master/lib/querystring.js

@drsm

What is your take on (URL native object) https://github.com/nginx/njs/issues/143 vs querystring.js.
URL is standardised (which is good) while querystring seems more useful for nginx usecases.

Should we implement both or URL is enough?

BTW, I noticed that there some web APIs. Body, Headers, Request, Response, of course including URL. Maybe it's welcome if the Nginx HTTP module supports the ways like them.
For example, Body has these methods like Body.json(), Body.text().
But it's a big change for the APIs of the HTTP module.
You mentioned that in future native http client is in the plan.
See the reference. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
In a few words. The style of web APIs is a good reference.

@xeioex

Should we implement both or URL is enough?

To me, there are two main use cases of querystring module:

  1. To handle a simple POST request like form submission out of the box.
  2. To send a POST subrequest to a legacy backend that can't handle anything except application/x-www-form-urlencoded.

But URL is enough. The problem is the iterator protocol

> var x = 'text=can%20i%20has%20ip&rpt=nnews2&grhow=clutop';
> var qs = require('querystring');
> qs.parse(x)
[Object: null prototype] {
  text: 'can i has ip',
  rpt: 'nnews2',
  grhow: 'clutop'
}
> var u = new URL('fake://')
> u.search = x;
> [...u.searchParams.entries()].reduce((a, e) => { a[e[0]] = e[1]; return a; }, {})
{ text: 'can i has ip', rpt: 'nnews2', grhow: 'clutop' }

@drsm

But URL is enough.

yes, in principle it is enough. Don't you think it is a bit ugly, to use a fake url to work with query string?

@xeioex

Don't you think it is a bit ugly, to use a fake url to work with query string?

Oh, actually, I miss a URLSearchParams() in the spec :). So it can be reduced to

[...(new URLSearchParams(x))].reduce((a, e) => { a[e[0]] = e[1]; return a; }, {})

@xeioex
@hongzhidao

In my opinion, Web APIs are over-complicated.
And without language features like async/await and iterators there will be a lot of boilerplate code.

So, having a simple memory effective alternative like querystring is a plus.

Was this page helpful?
0 / 5 - 0 ratings