Nodejs automatically generates folders every day according to the date. How should this be achieved? Ask the elder brother for advice

problem description: now there is a pc project, which is expected to be automatically saved under the generated folder after taking photos when it is used as a project for the first time every day. If you have this folder, you do not have to regenerate it into
example: when you use this system for the first time, the name of the generated folder is 20181205_photo (a date-photo folder is automatically generated every day)

every day, the event of "first click" to take a picture will automatically trigger a check to see if the file exists, and create

if the folder does not exist.
Jan.11,2022

node timer


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

// ,,
const getPhotoPath = () => {
  const date = new Date()
  const toPad2 = num => num.padStart(2, '0')
  const photoPath = path.join(
    __dirname,
    `${date.getFullYear()}${toPad2(`${date.getMonth() + 1}`)}${toPad2(
      `${date.getDate()}`
    )}_photo`
  )
  if (!fs.existsSync(photoPath)) {
    fs.mkdirSync(photoPath)
  }
  return photoPath
}

// ,`promise`
const write = (filePath, data) =>
  new Promise((resolve, reject) => {
    fs.writeFile(filePath, data, 'binary', err => {
      if (err) {
        reject(err)
      } else {
        resolve(true)
      }
    })
  })

// 
const onPhotograph = imgData => {
  const photoPath = getPhotoPath()
  write(path.join(photoPath, 'photo-name.img'), imgData).then(() => {
    // ... do something
  })
}
Menu