use express.js to get the query string of a url - javascript

I'm looking for a way to get the query string part of a url using node.js or one of it's extension module.
I've tried using url and express but they give me an array of parameters.
I want to original string.
Any ideas ?
example; for:
http://www.mydom.com?a=337&b=33
give me
a=337&b=33
(with or without the ?)

Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)
var url = require('url');
var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);
console.log(urlObj.query);
// => a=337&b=33

How about using the built-in url.parse method?

Related

How to Split Querystring in JS

I am trying to split the value from querystring using window.location.search after that I am getting the below value like
'?path=/dev/dev-alert-banner--alert-banner&args=alertBannerType:dark'
I tried to use like window.location.search.split('/').pop() but i am getting the value like dev-alert-banner--alert-banner&args=alertBannerType:dark
how can i get the value like
dev-alert-banner--alert-banner
using JS
Use the built-in URLSearchParams API to parse the QS for you - then you can do split with pop:
const value = '?path=/dev/dev-alert-banner--alert-banner&args=alertBannerType:dark';
const params = new URLSearchParams(value);
console.log(params.get("path").split("/").pop());

Javascript object with arrays to search param style query string

Looking for clean way to convert a javascript object containing arrays as values to a search param compatible query string. Serializing an element from each array before moving to the next index.
Using libraries such as querystring or qs, converts the object just fine, but handles each array independently. Passing the resulting string to the server (which I cannot change) causes an error in handling of the items as each previous value is overwritten by the next. Using any kind of array notation in the query string is not supported. The only option I have not tried is a custom sort function, but seems like it would be worse than writing a custom function to parse the object. Any revision to the object that would generate the expected result is welcome as well.
var qs = require("qs")
var jsobj = {
origString:['abc','123'],
newString:['abcd','1234'],
action:'compare'
}
qs.stringify(jsobj,{encode:false})
qs.stringify(jsobj,{encode:false,indices:false})
qs.stringify(jsobj,{encode:false,indices:false,arrayFormat:'repeat'})
Result returned is
"origString=abc&origString=123&newString=abcd&newString=1234&action=compare"
Result desired would be
"origString=abc&newString=abcd&origString=123&newString=1234&action=compare"
I tried reorder your json:
> var jsobj = [{origString: 'abc', newString: 'abcd' }, {origString: '123',
newString: '1234' }, {action:'compare'}]
> qs.stringify(jsobj,{encode:false})
'0[origString]=abc&0[newString]=abcd&1[origString]=123&1[newString]=1234&2[action]=compare'
But I don't know if this is a good alternative for your problem.
Chalk this up to misunderstanding of the application. After spending some more time with the API I realized my mistake, and as posted above by others, order does no matter. Not sure why my first several attempts failed but the question is 'answered'

Parse and extract after '=' in a string in JS [duplicate]

This question already has answers here:
How to get GET (query string) variables in Express.js on Node.js?
(26 answers)
Closed 5 years ago.
Let's say my url atm is
http://test.com/?city=toronto
I'm able to get the requestURL string that is
/?city=toronto
From here, I was wondering if there is a built in function or a standard procedure of extracting the word "toronto" or any other word that comes after the = from the string.
A standard procedure (as you mentioned) of doing this, you can get all the parameter values including value of city or any other parameter you may add to it.
var values = new URL('http://test.com/?city=toronto').searchParams.values();
for(var value of values){
console.log(value);
}
UPDATE
As #taystack mentioned in the comment, if you only want the value of a specific parameter (city) in this case, you can use:
new URL('http://test.com/?city=toronto').searchParams.get('city');
Use split();
var url = '/?city=toronto'.split('=');
console.log(url[1]);
Node.js has a new module called URL, which encodes the semantics of a url string. You don’t need to do any string manipulation.
const URL = require('url')
let my_url = new URL('http://test.com/?city=toronto')
URL#search returns the string representing the search:
my_url.search // '?city=toronto'
URL#query returns the search string excluding ?:
my_url.query // 'city=toronto'
and URL#searchParams returns an object encoding the search string:
my_url.searchParams // something kind of like {'city':'toronto'}
my_url.searchParams.get('city') // 'toronto'
my_url.searchParams.keys() // ['city'] (all the keys)
my_url.searchParams.values() // ['toronto'] (all the values)

How to back String from UUID in node-js

Using express-cassandra i am generating an uuid by uuidFromString() method. Is there any way to back it to its previous form.
Yes, just use the toString() method on the Uuid object you generated. For example:
var myUuid = Uuid.fromString('ce547c40-acf9-11e6-80f5-76304dec7eb7');
var myUuidString = myUuid.toString();
Since express-cassandra is just using the DataStax driver under the covers, you can see the Uuid docs here:
http://docs.datastax.com/en/drivers/nodejs/3.0/module-types-Uuid.html

How to retrieve value from object in JavaScript

Hi I am using a Java script variable
var parameter = $(this).find('#[id$=hfUrl]').val();
This value return to parameter now
"{'objType':'100','objID':'226','prevVoting':'" // THIS VALUE RETURN BY
$(this).find('[$id=hfurl]').val();
I want to store objType value in new:
var OBJECTTYPE = //WHAT SHOULD I WRITE so OBJECTTYPE contain 400
I am trying
OBJECTTYPE = parameter.objType; // but it's not working...
What should I do?
Try using parameter['objType'].
Just a note: your code snippet doesn't look right, but I guess you just posted it wrong.
Ok, not sure if I am correct but lets see:
You say you are storing {'objType':'100','objID':'226','prevVoting':' as string in a hidden field. The string is not a correct JSON string. It should look like this:
{"objType":100,"objID":226,"prevVoting":""}
You have to use double-quotes for strings inside a JSON object. For more information, see http://json.org/
Now, I think with $(this).find('[$id=hfurl]'); you want to retrieve that value. It looks like you are trying to find an element with ID hfurl,but $id is not a valid HTML attribute. This seems like very wrong jQuery to me. Try this instead:
var parameter = $('#hfurl').val();
parameter will contain a JSON string, so you have to parse it before you can access the values:
parameter = $.parseJSON(parameter);
Then you should be able to access the data with parameter.objType.
Update:
I would not store "broken" JSON in the field. Store the string similar to the one I shoed above and if you want to add values you can do it after parsing like so:
parameter.vote = vote;
parameter.myvote = vote;
It is less error prone.

Categories

Resources