diff --git "a/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo01.js" "b/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo01.js" new file mode 100644 index 0000000000000000000000000000000000000000..0fd6ad6fcaa55456130b7ee705079ca686a67ec4 --- /dev/null +++ "b/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo01.js" @@ -0,0 +1,11 @@ +let math=require("./demo02") + +try{ + console.log(math.handle(2,3,"add")); + console.log(math.handle(6,3,"subtract")); + console.log(math.handle(3,3,"multiply")); + console.log(math.handle(6,3,"divide")); + console.log(math.handle(6,0,"divide")); +}catch(error){ + console.log(error.message); +} \ No newline at end of file diff --git "a/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo02.js" "b/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo02.js" new file mode 100644 index 0000000000000000000000000000000000000000..46d13035c20d1d0615fc5b2f41c544451aeaeb5b --- /dev/null +++ "b/\351\273\204\346\266\246\345\261\261/2022-2-22nodejs/demo02.js" @@ -0,0 +1,28 @@ +var count = { + add:function(a,b) { + return a+b; + }, + subtract:function subtract(a,b){ + return a-b; + }, + multiply:function multiply(a,b){ + return a*b; + }, + divide:function divide(a,b){ + return a/b; + } + +} + +function handle(a,b,funcname){ + if(isNaN(a)||isNaN(b)){ + throw new Error("请输入数字"); + } + if(funcname=="divide"&&b===0){ + throw new Error("0不能做除数"); + } + return count[funcname](a,b) + +} + +module.exports.handle = handle; diff --git "a/\351\273\204\346\266\246\345\261\261/2022-2-24nodejs/demo01.js" "b/\351\273\204\346\266\246\345\261\261/2022-2-24nodejs/demo01.js" new file mode 100644 index 0000000000000000000000000000000000000000..574996f90392d109b226b6c49edb00a61e2d90f4 --- /dev/null +++ "b/\351\273\204\346\266\246\345\261\261/2022-2-24nodejs/demo01.js" @@ -0,0 +1,50 @@ +//写一段代码,可以指定的创建多个文件,文件名中要有序号,写入的内容可以随机 +let fs=require("fs") + +let createFile = { + /** + * 创建文件夹 + */ + createPath: function (dir) { + //文件夹可能存在 + if(fs.existsSync(dir)){ + return ; + } + fs.mkdirSync(dir); + }, + + /** + * 创建内容 + * + */ + createContent:function(num){ + return num+''; + }, + + /** + * 写入文件与内容 + * + */ + createFile:function(filePath,content){ + //flags w 可写 + console.log(filePath); + let fd = fs.openSync(filePath,'w'); + fs.writeSync(fd,content); + fs.closeSync(fd); + + }, + + handle:function(num,dir){ + //目录不存在则创建 + this.createPath(dir); + //循环的创建文件与内容 + for(let i=1;i