I
I
Ingerniated2017-04-02 13:44:49
Node.js
Ingerniated, 2017-04-02 13:44:49

How do the arguments in path work?

path has a resolve method, here is an example

const publicFolder = 'public';
const  pathname  = url.parse(req.url);
path.resolve(__dirname, publicFolder, pathname.slice(1) || 'index.html');

The documentation says that if the file is not found in these paths, it will access it in the root directory of the file, i.e. where is server.js.
But it exclusively looks for it only in the public folder
. Please tell me, are the first two arguments generally required in resolve? (__dirname, publicFolder)
And why isn't the documentation about pathname.slice(1)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2017-04-02
@Ingernirated

Most likely your code should look like this, otherwise it doesn't make sense:

const publicFolder = 'public';
const urlObject = url.parse(req.url);
path.resolve(__dirname, publicFolder, urlObject.pathname.slice(1) || 'index.html');

But who, where and for what should apply is not at all clear.
path.resolve - just concatenates the given arguments into a valid (syntactically) path, returns a string, and doesn't know anything about your files.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question