কম্পিউটার টিউটোরিয়াল

Node.js-এ Agent তৈরি করা

ভূমিকা

Node-এ new Agent() মেথড ব্যবহার করে এজেন্টের একটি ইনস্ট্যান্স তৈরি করা যায়। http.request() মেথড 'http' মডিউলের globalAgent ব্যবহার করে কাস্টম http.Agent ইনস্ট্যান্স তৈরি করে।

সিনট্যাক্স

new Agent({options})

প্যারামিটার

  • options — এজেন্ট তৈরির সময় কনফিগারেবল অপশনগুলো। নিচের ফিল্ড/অপশনগুলো থাকতে পারে:

    • keepAlive — আউটস্ট্যান্ডিং রিকোয়েস্ট না থাকলেও সকেটগুলো রাখে, ভবিষ্যতের রিকোয়েস্টের জন্য TCP কানেকশন পুনরায় স্থাপন ছাড়া। 'close' দিয়ে বন্ধ করা যায়। ডিফল্ট: false。

    • keepAliveMsecs — keepAlive true হলে TCP keep-Alive প্যাকেটের প্রাথমিক ডিলে। ডিফল্ট: 1000।

    • maxSockets — প্রতি হোস্টে অনুমোদিত সর্বোচ্চ সকেট সংখ্যা। ডিফল্ট: Infinity।

    • maxTotalSockets — সব হোস্টের জন্য মোট সকেট লিমিট। ডিফল্ট: Infinity।

    • maxFreeSockets — ফ্রি স্টেটে খোলা থাকতে পারে সর্বোচ্চ সকেট। ডিফল্ট: 256।

    • scheduling — নেক্সট ফ্রি সকেট পিক করার শিডিউলিং স্ট্র্যাটেজি: 'fifo' বা 'lifo'।

    • timeout — সকেট টাইমআউট মিলিসেকেন্ডে।

উদাহরণ ১

agent.js ফাইল তৈরি করে node agent.js রান করুন।

// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Creating a new agent var agent = new http.Agent({}); // Defining options for agent const aliveAgent = new http.Agent({   keepAlive: true, maxSockets: 5, }); // Creating connection with alive agent var aliveConnection = aliveAgent.createConnection; // Creating new connection var connection = agent.createConnection; // Printing the connection console.log('Succesfully created connection with agent: ', connection.toString); console.log('Succesfully created connection with alive agent: ', aliveConnection.toString);

আউটপুট

C:\home\node>> node agent.js Succesfully created connection with agent: function toString() { [native code] } Succesfully created connection with alive agent: function toString() { [native code] }

উদাহরণ ২ (agentkeepalive মডিউল ব্যবহার)

ইনস্টলেশন

npm install agentkeepalive --save

প্রোগ্রাম কোড

// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Importing the agentkeepalive module const Agent = require('agentkeepalive'); // Creating a new agent const keepAliveAgent = new Agent({}); // Implementing some options const options = {   host: 'tutorialspoint.com',   port: 80,   path: '/',   method: 'GET',   agent: keepAliveAgent, }; // Requesting details via http server module const req = http.request(options, (res) => {   // Printing statuscode, headers and other details   // received from the request   console.log("StatusCode: ", res.statusCode);   console.log("Headers: ", res.headers); }); // Printing the agent options console.log("Agent Options: ", req.agent.options); req.end();

আউটপুট

C:\home\node>> node agent.js Agent Options: { socketActiveTTL: 0,   timeout: 30000,   freeSocketTimeout: 15000,   keepAlive: true,   path: null } StatusCode: 403 Headers: { date: 'Sun, 25 Apr 2021 08:21:14 GMT',   server: 'Apache',   'x-frame-options': 'SAMEORIGIN',   'last-modified': 'Thu, 16 Oct 2014 13:20:58 GMT',   etag: '"1321-5058a1e728280"',   'accept-ranges': 'bytes',   'content-length': '4897',   'x-xss-protection': '1; mode=block',   vary: 'User-Agent',   'keep-alive': 'timeout=5, max=100',   connection: 'Keep-Alive',   'content-type': 'text/html; charset=UTF-8' }
  1. জাভাস্ক্রিপ্ট – Hello World

  2. জাভাস্ক্রিপ্টে বাইনারি ট্রি

  3. জাভাস্ক্রিপ্টে প্রিমের অ্যালগরিদম

  4. Node.js-এ Express-rate-limit ইন্টিগ্রেশন