XeNote/posts/Code/Node/Node.md
2020-11-28 19:36:28 +03:00

815 B

path Module

const notes = '/users/joe/notes.txt'

path.dirname(notes) // /users/joe
path.basename(notes) // notes.txt
path.extname(notes) // .txt

join

const name = 'joe'
path.join('/', 'users', name, 'notes.txt') //'/users/joe/notes.txt'

fs Module

Read the content of a directory

const fs = require('fs')
const path = require('path')

const folderPath = '/Users/joe'

fs.readdirSync(folderPath)

Get Full Path

fs.readdirSync(folderPath).map(fileName => {
  return path.join(folderPath, fileName)
})

Filter the Result - Only Files

const isFile = fileName => {
  return fs.lstatSync(fileName).isFile()
}

fs.readdirSync(folderPath).map(fileName => {
  return path.join(folderPath, fileName)
})
.filter(isFile)