انتقل إلى المحتوى

وحدة http — الجزء الأول

إعداد المشروع

  1. أنشئ مجلدًا جديدًا وسمّه "myserver".
  2. هيّئ المشروع باستخدام npm: npm init
  3. ثبّت وحدة http : npm install http
  4. أنشئ ملفًا جديدًا داخل المجلد "myserver" ، وسمّه «index.js".
  5. أضف الأسطر التالية إلى «index.js":
const http = require("http");

//create the server
const server = http.createServer((request, response) => {
    response.end("Hey, it's my first nodejs program");
});

server.listen(8000, () => console.log("The server is running on port 8000"));
  1. اكتب في الطرفية nodejs index.js.
  2. سترى «The server is running on port 8000 » في الطرفية.
  3. افتح المتصفح على العنوان http://localhost:8000.
  4. سترى «Hey, it's my first nodejs program» في المتصفح.

عبارة require

التمرين 1: ابدأ مشروعًا جديدًا باستخدام Node.js ، وسمّه «project1".

# Create a new folder, and name it "project1"
# Go to that "project1" directory
npm init

التمرين 2: ارجع إلى التمرين 1؛ أضف وحدة http إلى project1.

npm install http

التمرين 3: ارجع إلى التمرين 1؛ أنشئ ملفًا جديدًا داخل project1، وسمّه index.js. أضف http إلى index.js. تلميح: استخدم require('http').

const http = require('http');

require('package_name')

يمكنك تضمين وحدة أو حزمة في ملفك باستخدام require("module_name")

الكائن http

التمرين 4: ارجع إلى التمرين 3؛ اعرض http في وحدة التحكم.

const http = require('http');
console.log(http);

// Run nodejs index.js

ستحصل على كائن:

{
  METHODS: [
    'ACL',        'BIND',        'CHECKOUT',
  ],
  STATUS_CODES: {
    '100': 'Continue',
    '101': 'Switching Protocols',
    '511': 'Network Authentication Required'
  },
  Agent: [Function: Agent] { defaultMaxSockets: Infinity },
  Server: [Function: Server],
  ServerResponse: [Function: ServerResponse],
  createServer: [Function: createServer],
  get: [Function: get],
  request: [Function: request],
}

STATUS_CODES

التمرين 5: ارجع إلى التمرين 3؛ بما أن http كائن، فاعرض في وحدة التحكم معنى رمز الحالة 200. تلميح: تأمّل نتيجة التمرين 4 بعناية.

const http = require('http');

console.log(http.STATUS_CODES["200"]);

النتيجة هي: Ok

التمرين 6: ارجع إلى التمرين 3؛ بما أن http كائن، فاعرض في وحدة التحكم معنى رمز الحالة 500.

const http = require('http');

console.log(http.STATUS_CODES["500"]);

النتيجة هي: 'Internal Server Error'

التمرين 7: ارجع إلى التمرين 3؛ بما أن http كائن، فاعرض في وحدة التحكم معنى رمز الحالة 404.

const http = require('http');

console.log(http.STATUS_CODES["404"]);

النتيجة هي: 'Not Found'

التمرين 8: ارجع إلى التمرين 3؛ بما أن http كائن، فاعرض في وحدة التحكم معنى رمز الحالة 400.

const http = require('http');

console.log(http.STATUS_CODES["400"]);

النتيجة هي: 'Bad Request'

رمز حالة HTTP الوصف
200 Ok
404 غير موجود
400 طلب غير صالح
500 خطأ داخلي في الخادم

createServer()

التمرين 9: ارجع إلى التمرين 3؛ استدعِ createServer() على الكائن http.

const http = require('http');

const server = http.createServer();

التمرين 10: ارجع إلى التمرين 9؛ استدعِ listen(9000) على الكائن server . ثم شغّل nodejs index.js، وافتح العنوان http://localhost:9000 التالي في المتصفح.

const http = require('http');

const server = http.createServer();
server.listen(9000);

createServer()

العامل createServer() تُنشئ نسخة جديدة من الكائن http ، وتستقبل معاملين هما request و response .

server.listen(port)

العامل server.listen(port) تجعل الخادم يستمع إلى الطلبات على المنفذ المحدّد.

التمرين 11: أضف الشيفرة التالية إلى الملف index.js ، وشغّل nodejs index.js. ثم انتقل إلى http://localhost:9000 التالي في المتصفح.

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("Hello there");
});
server.listen(9000);

سترى هناك: Hello there في المتصفح.

مشروعات صغيرة

المشروع 1

التمرين 12: أنشئ خادم Node.js يستمع على المنفذ "5000"، ويعرض الرسالة "I'm running on port 5000, yesss!" في نافذة المتصفح.

mkdir project1 # Create a folder for the project
cd project1
npm init
npm install http
touch index.js # create index.js

اكتب ما يلي في index.js:

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("I'm running on port 5000, yesss!");
});
server.listen(5000);

المشروع 2

التمرين 13: أنشئ خادم Node.js يستمع على المنفذ "8887"، ويعرض الرسالة "Ok, I'm doing it again!" في نافذة المتصفح.

mkdir project2 # Create a folder for the project
cd project2
npm init
npm install http
touch index.js # create index.js

اكتب ما يلي في index.js:

const http = require('http');

const server = http.createServer((request, response) => {
    response.end("Ok, I'm doing it again!");
});
server.listen(8887);