Node.js—小试牛刀-创建目录

今天第一天学习Node.js  感觉特别亲切

//文件结构
//引入模块
const path=require('path')
const fs=require('fs')


let roots='H:\\前端学习\\nodejs'
let initData = {
    projectName: 'mydome',
    data: [{
        name: 'images',
        type: 'dir'
    },{
            name: 'css',
            type: 'dir'
        }, {
            name: 'js',
            type: 'dir'
        }, {
            name: 'index.html',
            type: 'file'
        }
    ]
}
const content='<!DOCTYPE html>\n' +
    '<html lang="en">\n' +
    '<head>\n' +
    '    <meta charset="UTF-8">\n' +
    '    <title>Title</title>\n' +
    '</head>\n' +
    '<body>\n' +
    '\n' +
    '</body>\n' +
    '</html>'
//创建。目录
fs.mkdir(path.join(roots,initData.projectName),(err) => {
    if (err) return;
  initData.data.forEach((item)=>{
      if (item.type=='dir'){
          fs.mkdirSync(path.join(roots,initData.projectName,item.name))
      }
      else if (item.type=='file'){
          fs.writeFileSync(path.join(roots,initData.projectName,item.name),content)
      }
  })
    console.log('创建成功')
})

  学习关键是学习对象的方法和属性

猜你喜欢

转载自www.cnblogs.com/ruogu/p/10922125.html