• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
Node js Intro
node xx.js; process.argv; Use file system to creat folder and files; const fs = require('fs'); mkdirSync; writeFileSync

帮助文档: https://nodejs.org/dist/latest-v14.x/docs/api/

 

  • 运行node js
for (let i = 0; i < 10; i++) {
    console.log("大柱睡觉了吗✨😴")
}

  • process.argv

The process.argv property returns an array containing the command-line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.

console.log("HELLO FROM ARGS FILE!")
console.log(process.argv)


const args = process.argv.slice(2);
for (let arg of args) {
    console.log(`Hi there, ${arg}`)
}

// print process.argv
process.argv.forEach((val, index) => {
    console.log(`${index}: ${val}`);
});

  • Use file system to creat folder and files example

File system: https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_file_system

The fs module enables interacting with the file system in a way modeled on standard POSIX functions.

To use this module:

const fs = require('fs');

All file system operations have synchronous, callback, and promise-based forms.

const fs = require('fs');//necessary!!!!
const folderName = process.argv[2] || 'Project'//folderName是第三个parameter,默认值:Project

//callback
// fs.mkdir('Dogs', { recursive: true }, (err) => {
//     console.log("IN THE CALLBACK!!")
//     if (err) throw err;
// });


//Synchronous
try {
    fs.mkdirSync(folderName);//create new folder
    fs.writeFileSync(`${folderName}/index.html`, '')//create new html file
    fs.writeFileSync(`${folderName}/app.js`, '')//create new js file
    fs.writeFileSync(`${folderName}/styles.css`, '')//create new css file
} catch (e) {
    console.log("SOMETHING WENT WRONG!!!");
    console.log(e);
}

 

posted on 2021-01-30 01:48  LilyLiya  阅读(106)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3