The Path Module¶
Basic Methods¶
Task 1: Create a simple nodejs server.
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {"Content-Type": "text/plain"})
res.end('Hello there')
});
server.listen(9000);
Task 2: Refer to task 1; add the path module to your file. Hint: use the require
statement.
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);
Task 3: Check the following code.
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);
You can see style.css in the browser.
Task 4: Refer to task 3; set the value of the portion
to 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);
You will also see style.css in the browser.
path.basename()
The path.basename()
method returns the last part of a given path.
Task 5: Check the following code.
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);
You can see ./public/css in the browser.
Task 6: Refer to task 5; set the value of the portion
to 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);
You will also see ./public/css in the browser.
path.dirname()
The path.dirname()
method returns the directory of a given path.
Task 7: Check the following code.
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);
You can see .css in the browser.
Task 8: Refer to task 5; set the value of the portion
to 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);
You will also see .css in the browser.
path.extname()
The path.extname()
method returns the file extension of a given path.
Task 9: check the following code.
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);
You will see the following in the browser:
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()
The path.parse()
method returns an object that holds information about a given path. For instance, it returns: {root: '/', dir: '/public/css', base: 'style.css', ext: '.css', name: 'style'}
Task 10: Check the following code.
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);
You will see /public/js/myscript.js in the browser.
Task 11: Refer to task 10; send res.end(path.join(rootPath, dirPath, basePath))
as a response instead of the previous response.
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);
You will see /public/js/myscript.js in the browser.
path.join()
The path.join()
method joins two or more parts of a path.
__filename¶
Task 12: Create a simple javascript file, and log the __filename
object. Run it with nodejs yourfile.js
.
console.log(__filename);
You will get something similar to this: /absolute/dir/to/your/file.js
Task 13: Create another file, and log the __filename
object in it.
console.log(__filename);
You will get something similar to this: /the/dir/to/anotherFile.js
__filename
The __filename
returns the absolute path of the currently executed file with the file name itself.
__dirname¶
Task 14: Create a simple javascript file, and log the __dirname
object. Run it with nodejs yourfile.js
.
console.log(__dirname);
You will get something similar to this: the/absolute/dir
Task 15: Create another file, and log the __dirname
object in it.
console.log(__dirname);
You will get something similar to this: /the/dir/to
__dirname
The __dirname
returns the absolute path of the currently executed file.
Mini Projects¶
Project 1¶
Task 16: Create a simple nodejs server. And display the current directoy + "public" + the requested url by the user
in the browser.
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);
If the user hits http://localhost:9000/style.css
, "/absolute/dir/public/style.js" will be displayed on the browser.
Project 2¶
Task 17: Create a simple nodejs server. And display the current directoy + the requested url by the user
in the browser.
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);
If the user hits http://localhost:9000/script.js
, "/absolute/dir/script.js" will be displayed on the browser.