Validate path nodejs

1
Hello, I'm developing a file manipulation system, and I just want to be able to manipulate folder with an absolute path eg /opt/file . In other words, I can only create / edit / delete folders on this path. I want to validate that the start path is always / opt / file.

What is the best way to do this? Thanks

    
asked by anonymous 11.12.2018 / 21:57

1 answer

1

Since the path is a string you can use the indexOf in question starts with the text that was provided. It would look something like this:

...
if (path.indexOf('/opt/file') === 0) {
  // Faz o que deve fazer com o arquivo
...
  

string

     

The indexOf method returns the index of the first occurrence of the value specified in searchValue within the indexOf() object to which it was called, starting the search from String . Returns -1 if the value is not found.

     

Syntax

str.indexOf(searchValue[, fromIndex])
     

searchValue

     

A string representing the value to be fetched.

     

fromIndex

     

The position of the original string from which the search should begin. It can be any integer. The default value is 0. If fromIndex the entire string is traversed (equivalent to passing 0). If fromIndex < 0 , the method will return -1.

    
12.12.2018 / 12:50