I am learning some node.js from Eloquent JavaScript where I found this piece of code whose purpose is to decode the url requested to a server and to resolve it relative to the server’s working directory:
const {parse} = require("url");
const {resolve, sep} = require("path");
const baseDirectory = process.cwd();
function urlPath(url) {
let {pathname} = parse(url);
let path = resolve(decodeURIComponent(pathname).slice(1));
if (path != baseDirectory &&
!path.startsWith(baseDirectory + sep)) {
throw {status: 403, body: "Forbidden"};
}
return path;
}
The book doesn't say anything about the .slice(1) in the line
let path = resolve(decodeURIComponent(pathname).slice(1));
Why not do just slice()? Why skip the first character of the decoded URL before comparing it with the path of the current working directory?