وحدة http — الجزء الثاني¶
كائن الطلب¶
التمرين 1: أنشئ خادم Node.js يستمع على المنفذ "8000"، ويعرض الرسالة "Ok, it works!" في نافذة المتصفح.
const http = require('http');
const server = http.createServer((request, response) => {
response.end("Ok, it works!");
});
server.listen(8000);
التمرين 2: ارجع إلى التمرين 1؛ اطبع request.url في وحدة التحكم.
const http = require('http');
const server = http.createServer((request, response) => {
console.log(request.url);
response.end("Ok, it works!");
});
server.listen(8000);
النتيجة هي:
//hit http://localhost:8000/
/
//hit http://localhost:8000/anything
/anything
//hit http://localhost:8000/something/here
/something/here
request.url
العامل request.url تُرجع الجزء من العنوان الذي يطلبه المستخدم في المتصفح.
التمرين 3: ارجع إلى التمرين 1؛ اطبع request.method في وحدة التحكم.
const http = require('http');
const server = http.createServer((request, response) => {
console.log(request.method);
response.end("Ok, it works!");
});
server.listen(8000);
النتيجة هي:
//hit http://localhost:8000/
GET
//hit http://localhost:8000/anything
GET
//hit http://localhost:8000/something/here
GET
request.method
العامل request.method تُرجع طريقة الطلب، مثل GET أو POST.
التمرين 4: أنشئ خادم Node.js. إذا طلب المستخدم /، فاعرض <h1>Welcome to the homepage</h1>. وفي غير ذلك اعرض <h1>Page Not Found</h1>.
const http = require('http');
const server = http.createServer((request, response) => {
if(request.url === '/'){
response.end('<h1>Welcome to the homepage</h1>')
}else{
response.end("<h1>Page Not Found</h1>");
}
});
server.listen(8000);
التمرين 5: أنشئ خادم Node.js. إذا طلب المستخدم /، فاعرض <h1>Welcome to the homepage</h1>، وإذا طلب المستخدم /about، فاعرض <h1>About Page</h1>. وفي غير ذلك اعرض <h1>Page Not Found</h1>.
const http = require('http');
const server = http.createServer((request, response) => {
if(request.url === '/'){
response.end('<h1>Welcome to the homepage</h1>')
}else if(request.url === '/about'){
response.end('<h1>About Page</h1>');
}else{
response.end("<h1>Page Not Found</h1>");
}
});
server.listen(8000);
response.writeHead()¶
التمرين 6: تأمّل الشيفرة التالية. إذا طلب المستخدم /, Hello there فستظهر في نافذة المتصفح، وسيُضبط رمز حالة الاستجابة على 200، وتُضبط الترويسة Content-Type على text/plain.
const http = require('http');
const server = http.createServer((request, response) => {
if(request.url === '/'){
response.writeHead(200, {"Content-Type": "text/plain"})
response.end('Hello there')
}
});
server.listen(8000);
response.writeHead()
العامل response.writeHead() تتيح إرسال ترويسات الاستجابة، مثل رمز حالة HTTP ونوع محتوى الاستجابة.
التمرين 7: ارجع إلى التمرين 6؛ إذا كان العنوان المطلوب مختلفًا عن /، فاعرض "Not Found"، واضبط رمز حالة HTTP على 404، واضبط Content-Type على text/plain.
const http = require('http');
const server = http.createServer((request, response) => {
if(request.url === '/'){
response.writeHead(200, {"Content-Type": "text/plain"})
response.end('Hello there')
}else{
response.writeHead(404, {"Content-Type": "text/plain"});
response.end('Not Found');
}
});
server.listen(8000);
مشروعات صغيرة¶
المشروع 1¶
التمرين 8: أنشئ خادم Node.js يُرسل الاستجابات الموضّحة في الجدول التالي:
| العنوان المطلوب | الرسالة التي ستظهر في المتصفح | رمز استجابة HTTP | Content-Type |
|---|---|---|---|
| / | <h1>You have reached the root URL</h1> | 200 | "text/html" |
| /admin | You have reached the admin area | 200 | "text/plain" |
| أي عنوان آخر | غير موجود | 404 | "text/plain" |
const http = require('http');
const server = http.createServer((req, res) => {
if(req.url === "/"){
res.writeHead(200, {"Content-Type": "text/html"});
res.end("<h1>You have reached the root URL</h1>");
}else if(req.url === "/admin"){
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("You have reached the admin area");
}else{
res.writeHead(404, {"Content-Type": "text/plain"});
res.end("Not Found");
}
});
server.listen(8000);
المشروع 2¶
التمرين 9: أنشئ خادم Node.js يُرسل الاستجابات الموضّحة في الجدول التالي:
| العنوان المطلوب | الرسالة التي ستظهر في المتصفح | رمز استجابة HTTP | Content-Type |
|---|---|---|---|
| / | <h1>Hey welcome here!</h1> | 200 | "text/html" |
| /restrictive | <h2>You can't get into here</h2> | 403 | "text/html" |
| أي عنوان آخر | غير موجود | 404 | "text/plain" |
const http = require('http');
const server = http.createServer((req, res) => {
if(req.url === "/"){
res.writeHead(200, {"Content-Type": "text/html"});
res.end("<h1>Hey welcome here!</h1>");
}else if(req.url === "/restrictive"){
res.writeHead(403, {"Content-Type": "text/html"});
res.end("<h2>You can't get into here</h2>");
}else{
res.writeHead(404, {"Content-Type": "text/plain"});
res.end("Not Found");
}
});
server.listen(8000);