Modern Full-Stack Development


HTTP and HTTPS (http and https)



Download 5,64 Mb.
Pdf ko'rish
bet34/107
Sana06.08.2021
Hajmi5,64 Mb.
#140576
1   ...   30   31   32   33   34   35   36   37   ...   107
Bog'liq
Modern Full-Stack Development Using TypeScript, React, Node

 HTTP and HTTPS (http and https)
Although Node isn’t exclusively for writing servers, whether web servers or other kinds 
of servers, it is, in fact, best known for writing web servers (or, to be more precise, HTTP 
servers). As such, you’d reasonably expect to find one or more modules related to such 
activities, and you would be very right to do so!
The HTTP module is where you’ll find most of what you need to write servers that 
use the HTTP protocol. If you’re into the whole security thing (and, uhh, you are into the 
whole security thing…right?!), then there is also an HTTPS module.
You already saw the http.Server class, if only briefly, in the last chapter, because 
it’s what you get back from the http.createServer() method, which is one method 
available in the HTTP package. You saw how to call the listen() method on that  
http.Server instance, but you can also find out if the instance is listening by 
interrogating its listen property (a simple boolean). You can also call the close() 
method on the instance to stop it from accepting requests. You can call setTimeout() on 
it to set how long a socket to a client lives for (two minutes by default) and you can set 
the maxHeadersCount property to limit how many HTTP headers your server will accept 
(defaults to 2000).
The HTTPS module builds on top of the HTTP module and provides the ability to 
create servers that use TLS to secure connections. The only real difference is some extra 
information about certificates and keys that you must provide when constructing the 
server:
const fs = require("fs");
const server = require("https").createServer(
  {
    key : fs.readFileSync("my_key.pem"),
    cert : fs.readFileSync("my_cert.pem")
  },
Chapter 2   a Few More words: advanCed node and npM


33
  (inRequest, inResponse) => {
  inResponse.writeHead(200);
  inResponse.end("I am protected by TLS!");
}).listen(443);
Aside from that, the API available is virtually identical.
But the HTTP and HTTPS modules aren’t just about servers: they also provide the 
means for your Node-based code to make HTTP requests! Here is an example, using the 
http.request() method:
let finalResponse = "";
const request = require("http").request(
  {
    hostname : "www.some_remote_system.com",
    port : 80,
    path : "/someAPI",
    method : "POST"
  },
  (inResponse) => {
    console.log(`STATUS: ${inResponse.statusCode}`);
    inResponse.setEncoding("utf8");
    inResponse.on("data", (inDataChunk) => {
      finalResponse += inDataChunk.
    });
    inResponse.on("end", () => {
      console.log(finalResponse);
    });
  }
);
request.write("Some data to send to the remote system");
request.end();
Chapter 2   a Few More words: advanCed node and npM


34
Here, we build up an http.Request object, providing to it all the pertinent 
connection details in the first object passed to the http.request() method. Then, we 
send some arbitrary data string to the remote server and build up a response string. 
The on event fires every time the server sends back a chunk of data, so we add it to 
the finalResponse string, which is displayed when the end event fires, signifying the 
complete response has been received. We are, of course, free to do whatever we wish 
with the response at that point.

Download 5,64 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   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