Modern Full-Stack Development


Query Strings (querystring)



Download 5,64 Mb.
Pdf ko'rish
bet38/107
Sana06.08.2021
Hajmi5,64 Mb.
#140576
1   ...   34   35   36   37   38   39   40   41   ...   107
Bog'liq
Modern Full-Stack Development Using TypeScript, React, Node

 Query Strings (querystring)
When building HTTP(S) servers with Node, having to examine the query string is 
commonly needed (I mean that’s not limited to just Node of course: dealing with query 
strings, either parsing them or creating them, is a big part of what web developers do!). 
The Query String module offers a handful of methods for such times.
When talking about query strings on the input side of the fence, querystring.parse() 
is the primary tool in your toolbox (assume this code is within a server event handler as 
you’ve previously seen, and the URL in the comment is the request that came in):
// "http://mysite.com/?account=36764&add=125";
let parsedURL = url.parse(request.url);
let parsedParams = querystring.parse(parsedURL);
Now, you’re getting a bonus look-ahead here to the next module we’ll be checking 
out: URL. The url.parse() method takes in a URL and returns an object like so:
{
    protocol: 'http:',
    slashes: true,
    auth: null,
    host: 'mysite.com',
    port: null,
    hostname: 'mysite.com',
    hash: null,
    search: '?account=36764&add=125',
    query: 'account=36764&add=125',
    pathname: '/',
    path: '/?account=36764&add=125',
    href: 'http://mysite.com/?account=36764&add=125'
}
All by itself, that’s useful, but if all we’re interested in are those query parameters 
account and add, then that’s where the querystring.parse() method comes in. What 
you get back from that is an object:
{ account : "36764", add : "125" }
Chapter 2   a Few More words: advanCed node and npM


38
From there out, you can do whatever you like with the data. Note that there is also a 
querystring.decode(), which is just an alias for querystring.parse().
What about if you need to construct a query string? Well, that’s where the 
querystring.stringify() method (and its querystring.encode() alias) fit in:
const qs = querystring.stringify({
  account : 36764, add : 125
});
// Returns "account=36764&add=125"
Finally, there are the querystring.escape() and querystring.unescape() 
methods, which are the yin and yang of encoding and decoding parameter names and 
values in query strings so that they are safe for inclusion in URLs. Note that you typically 
wouldn’t call these directly as they’re used by querystring.parse() and querystring.
stringify() internally and automatically, but they are there if you need them.
Well, how about that. This is the first (and only) module where everything it offers is 
described here! It’s not a big module, but it’s a somewhat important one!

Download 5,64 Mb.

Do'stlaringiz bilan baham:
1   ...   34   35   36   37   38   39   40   41   ...   107




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish