# nodejs_practice **Repository Path**: duoduo3_69/nodejs_practice ## Basic Information - **Project Name**: nodejs_practice - **Description**: nodejs 练习 - **Primary Language**: CoffeeScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2014-01-12 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README node === 同步还是异步 --- node 有同步和异步的方法,同步的后面有 Sync的后缀,异步使用回调函数,推荐用异步, 异步不会阻塞IO 模块化 --- 模块用module.exports 使用异步回调方式,以过滤显示文件列表为例 df 使用方式 df("uri","文件后缀",callback) # 打印/opt/node下所有的js df( "/opt/node", ".js", function(error,files){ for (var i = 0,len = files.lenght; i < len; i++ ){ console.log(files[i]) } }) # code fs = require("fs") path = require("path") df = (dir, ext, callback) -> fs.readdir dir, (error, files)-> return callback error if error fs = (f for f in files when path.extname(f) == ext) return callback(null,fs) module.exports = df 如果想将df当作模块,可以这么写 module.exports.df = df # 外部 model = require("./df") # model.df(path,ext,callback) module.exports.a = df # 外部 model require("./df") # model.a(path,ext,callback)