Why to skip first character of URL

Viewed 52

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?

1 Answers

The first character of the pathname will be / and the goal is to use a relative path from the current working directory and not an absolute path starting at the root of the file system.

Related