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

وحدة path

الطرائق الأساسية

التمرين 1: أنشئ خادم Node.js بسيطًا.

const http = require('http');

const server = http.createServer((req, res) => {
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end('Hello there')
});

server.listen(9000);

التمرين 2: ارجع إلى التمرين 1؛ أضف وحدة path إلى ملفك. تلميح: استخدم require .

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end('Hello there')
});

server.listen(9000);

التمرين 3: تأمّل الشيفرة التالية.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = 'style.css';
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى style.css التالي في المتصفح.

التمرين 4: ارجع إلى التمرين 3؛ اضبط قيمة portion على path.basename(file).

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = path.basename(file);
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى أيضًا style.css التالي في المتصفح.

path.basename()

العامل path.basename() تُرجع الجزء الأخير من المسار المُعطى.

التمرين 5: تأمّل الشيفرة التالية.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = './public/css';
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى ./public/css التالي في المتصفح.

التمرين 6: ارجع إلى التمرين 5؛ اضبط قيمة portion على path.dirname(file).

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = path.dirname(file);
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى أيضًا ./public/css التالي في المتصفح.

path.dirname()

العامل path.dirname() تُرجع مجلد المسار المُعطى.

التمرين 7: تأمّل الشيفرة التالية.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = 'css';
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى .css التالي في المتصفح.

التمرين 8: ارجع إلى التمرين 5؛ اضبط قيمة portion على path.extname(file).

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let portion = path.extname(file);
    res.writeHead(200, {"Content-Type": "text/plain"})
    res.end(portion);
});

server.listen(9000);

سترى أيضًا .css التالي في المتصفح.

path.extname()

العامل path.extname() تُرجع امتداد الملف في المسار المُعطى.

التمرين 9: تأمّل الشيفرة التالية.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let file = './public/css/style.css';
    let pathObject = path.parse(file);
    console.log(pathObject);
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(`Hello, you provided me with the "${file}" path. The Path root is "${pathObject.root}". The path directory is "${pathObject.dir}". The path base is "${pathObject.base}". The path extension is "${pathObject.ext}". The path name is "${pathObject.name}"`);
});

server.listen(9000);

سترى ما يلي في المتصفح:

Hello, you provided me with the "./public/css/style.css" path. The Path root is "". The path directory is "./public/css". The path base is "style.css". The path extension is ".css". The path name is "style"

path.parse()

العامل path.parse() تُرجع كائنًا يحمل معلومات عن المسار المُعطى. مثلًا، تُرجع: {root: '/', dir: '/public/css', base: 'style.css', ext: '.css', name: 'style'}

التمرين 10: تأمّل الشيفرة التالية.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let rootPath = "/";
    let dirPath = "public/js";
    let basePath = "myscript.js"; 
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(`${rootPath}${dirPath}/${basePath}`);
});

server.listen(9000);

سترى /public/js/myscript.js التالي في المتصفح.

التمرين 11: ارجع إلى التمرين 10؛ أرسل res.end(path.join(rootPath, dirPath, basePath)) استجابةً بدلًا من الاستجابة السابقة.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let rootPath = "/";
    let dirPath = "public/js";
    let basePath = "myscript.js"; 
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(path.join(rootPath, dirPath, basePath));
});

server.listen(9000);

سترى /public/js/myscript.js التالي في المتصفح.

path.join()

العامل path.join() تجمع جزأين أو أكثر في مسار واحد.

__filename

التمرين 12: أنشئ ملف JavaScript بسيطًا واطبع قيمة __filename . شغّله باستخدام nodejs yourfile.js.

console.log(__filename);

ستحصل على نتيجة مشابهة لما يلي: /absolute/dir/to/your/file.js

التمرين 13: أنشئ ملفًا آخر واطبع قيمة __filename فيه.

console.log(__filename);

ستحصل على نتيجة مشابهة لما يلي: /the/dir/to/anotherFile.js

__filename

العامل __filename تُرجع المسار المطلق للملف الجاري تشغيله، بما في ذلك اسم الملف نفسه.

__dirname

التمرين 14: أنشئ ملف JavaScript بسيطًا واطبع قيمة __dirname . شغّله باستخدام nodejs yourfile.js.

console.log(__dirname);

ستحصل على نتيجة مشابهة لما يلي: the/absolute/dir

التمرين 15: أنشئ ملفًا آخر واطبع قيمة __dirname فيه.

console.log(__dirname);

ستحصل على نتيجة مشابهة لما يلي: /the/dir/to

__dirname

العامل __dirname تُرجع المسار المطلق للمجلد الذي يحتوي على الملف الجاري تشغيله.

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

المشروع 1

التمرين 16: أنشئ ملفًا بسيطًا باستخدام Node.js خادمًا، واعرض the current directoy + "public" + the requested url by the user التالي في المتصفح.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let result = path.join(__dirname, "public", req.url)
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(result);
});

server.listen(9000);

إذا طلب المستخدم http://localhost:9000/style.css, "/absolute/dir/public/style.js" فسيظهر في المتصفح.

المشروع 2

التمرين 17: أنشئ ملفًا بسيطًا باستخدام Node.js خادمًا، واعرض the current directoy + the requested url by the user التالي في المتصفح.

const http = require('http');
const path = require('path');

const server = http.createServer((req, res) => {
    let result = path.join(__dirname, req.url)
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(result);
});

server.listen(9000);

إذا طلب المستخدم http://localhost:9000/script.js, "/absolute/dir/script.js" فسيظهر في المتصفح.