Node js Intro

帮助文档: 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);
}

原文地址:https://www.cnblogs.com/LilyLiya/p/14347846.html