The parse module in querystring lib lists the following.
querystring.parse(str[, sep[, eq[, options]]])
I've also seen the following code
const { parse } = require('querystring')
...
let actual = parse(queryString)[queryStringPropName]
Why is the array appended after parse(queryString) and more importantly, why does it work?
The array-appending was not seen in querystring's API page in NodeJS.
Can anyone explain?
That's because parse(queryString) is an object and in this case, [queryStringPropName] is not an array, it is to access a prop of the object parse(queryString)
Related
I am doing Google Oath2 Implementation. For a particular authorization_code, I was constantly getting invalid_grant. I checked the value and found that the query string value was encoded.
Here is an example:
const parser = require('url');
url ='http://example.com/test?param=4%2F12'
const q = parser.parse(url, true).query
console.log(q)
My output here is
{ param: '4/12' }
I want my output to be
{ param: '4%2F12' }
as the correct auth code is a string with value 4%2F12. How do I implement this?. There may be
many manual ways to do this. Anything that needs a minimalistic code effort would be appreciated. Thanks in advance!
Simple. Just encode again the param using encodeURIComponent.
Example:
console.log(encodeURIComponent("4/12")) // Output: 4%2F12
By using tab in Nodejs REPL I could get the information of a module, e.g. module url like below. But what if I want to know the details of a function in it? e.g. I want to know what parameters the url.parse function need, and the details of them. Can I get this information offline from REPL?
> var url = require('url');
undefined
> url.
url.__defineGetter__ url.__defineSetter__ url.__lookupGetter__
url.__lookupSetter__ url.__proto__ url.constructor
url.hasOwnProperty url.isPrototypeOf url.propertyIsEnumerable
url.toLocaleString url.toString url.valueOf
url.URL url.Url url.domainToASCII
url.domainToUnicode url.format url.originFor
url.parse url.resolve url.resolveObject
Try calling url.parse.toString() this will give you the 'source' of the function.
In case of url.parse this technique returns (in node 6.9.2 on Ubuntu)
'function urlParse(url, parseQueryString, slashesDenoteHost) {\n if (url instanceof Url) return url;\n\n var u = new Url();\n u.parse(url, parseQueryString, slashesDenoteHost);\n return u;\n}'
I'm attempting to scan an XML file for a specific "attribute". This is the file: The image
I'd like to parse it for the "file_url" attribute, but I have no clue to how.
If anyone can help me, It'd be very much appreciated.
You can use node xml2js module for parsing
Code is as follows:-
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
// you can access file url as given below
console.dir(result.file_url);
});
Xml is converted into javascript object so you can access each attributes just like object properties of js.
I'm trying to get param from request object using following code.
module.exports = function (req, res) {
var query = req.query;
var data = JSON.parse(query.param1);
}
This is working fine for most of the cases.
If param1 contains & character then query.param1 get values before & and next values are considered as new param.
eg
localhost/?param1={"url":"http://s.test.com/x?format=jsonp&id=a&callback=b"}
Edit original url is already encoded
localhost/?param1=%7B%22url%22%3A%22http%3A%2F%2Fs.test.com%2Fx%3Fformat%3Djsonp%26id%3Da%26callback%3Db%22%7D
in this case I'm getting param1 = {"url":"http://s.test.com/x?format=jsonp
which is not valid json, so I'm getting error, currently we've solved it using regex (removing localhost/?param1= part of url).
What's best way to handle this use case?
Edit : server environment
centos 6.5 server
node v0.12.7
express#4.13.3
req.query is already an object with express :
An object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.
req.query documentation
You have to encode your URL before calling nodejs API. And on the nodejs side, you have to decode the URL to get correct parameteres.
I'm trying to use breeze.js with sails.js.
Therefore I'm using the breeze json uriBuilder.
I logged the req.query and got the following:
{ '{"where":{"name":"foo"},"orderBy":': { '"name"': '' } }
To query waterline objects I need to bring it into a format like this:
{ where: { name: 'foo' }, sort: 'name' }
First thing I tried:
var queryString = JSON.parse(Object.keys(req.query)[0]);
That works as long as I only put a where clause in. But with more parameters i get this strangely formatted json object.
How can I parse it to get the correct object?
Solution:
Don't parse he req.query. Parse the url and parse it. This way u will get a json uery that sails accepts. Now strip of unsupported parameters as select and you're done.
var parsedUrl = urlUtils.parse(req.url, true);
var jsonQueryString = Object.keys(parsedUrl.query)[0];
var jsonQuery = JSON.parse(jsonQueryString);
Good question! We haven't yet documented this adequately, but the basic idea is that there is a breeze-client npm package (https://www.npmjs.com/package/breeze-client) that you can use to parse the incoming json query into a breeze EntityQuery instance, from there you can interrogate the EntityQuery and turn it into whatever server side syntax you want, i.e. in your case Sails.)
// req = the HTTP request object.
var resourceName = req.params.slug; // the HTTP endpoint
var entityQuery = breeze.EntityQuery.fromUrl(req.url, resourceName);
// you would write the transform to sails below.
var sailsQuery = tranformToSails(entityQuery);
This is exactly what we do in the breeze-sequelize npm package where we take the incoming req.query and go thru the process above to create a 'Sequelize' query.
See http://www.getbreezenow.com/sequelize-mysqlpostgressql-lite