nodeschool.io 6
~~ MAKE IT MODULAR ~~
This problem is the same as the previous but introduces the concept of
modules. You will need to create two files to solve this.
Create a program that prints a list of files in a given directory,
filtered by the extension of the files. The first argument is the
directory name and the second argument is the extension filter. Print
the list of files to the console. You must use asynchronous I/O.
Your program must use a module to do most of the work. The module
must export a single function that takes three arguments: the
directory name, the filter string and a callback function.
The callback must return an error, and only an error, as the first
argument if one is passed from your call to `fs.readdir()`. If there
are no errors then the first argument to the callback must be null and
the second must be your filtered list of files in an array.
In the case of an error bubbling up to your original program file,
simply check for it and print an informative message to the console.
----------------------------------------------------------------------
HINTS:
Create a new module by creating a new file that just contains your
directory reading and filtering function. To define a single function
export you assign your function to the `module.exports` object,
overwriting what is already there:
module.exports = function (...) { ... }
Or you can use a named function and assign the name.
To use your new module in your original program file, use the
`require()` call in the same way that you `require('fs')` to load the
`fs` module. The only difference is that for local modules must be
prefixed with './'. So, if your file is named mymodule.js then:
var mymodule = require('./mymodule.js')
The '.js' is optional here and you will often see it omitted.
You now have the `module.exports` object in your module assigned to
the `mymodule` variable. Since you are exporting a single function,
`mymodule` is a function you can call!
Also keep in mind that it is idiomatic to check for errors and do
early-returns within callback functions:
foo(function (err, data) {
if (err)
return callback(err)
... // continue when no-error
})
----------------------------------------------------------------------
myModule.js
- module.exports = function(dirName,regexStr,foo) {
- var fs = require('fs');
- var regex = new RegExp('\\.' + regexStr + '$')
- fs.readdir(dirName, function (err, list) {
- if(err) return foo(err)
- list = list.filter(function (file) {
- return regex.test(file);
- })
- foo(null,list);
- })
- }
myRequire.js
- var mymodule = require("./mymodule.js"),
- dirName = process.argv[2],
- regexStr = process.argv[3];
- mymodule(dirName,regexStr,function(err,list){
- if(err) console.log(err);
- list.forEach(function (file) {
- console.log(file);
- })
- });
正如第5题一样,forEach 我平时基本不用,还有参数里面是function的,在执行的时候定义,我平时也不太容易想起来。
nodeschool.io 6的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- AOD.net
ADO.NET中的五个主要对象 Connection 物件Connection 对象主要是开启程序和数据库之间的连结.没有利用连结对象将数据库打开,是无法从数据库中取得数据的.这个物件在ADO.NET ...
- SetWindowHookEx 做消息响应
HHOOK g_Hook = NULL; LRESULT CALLBACK CallWndProc( _In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM l ...
- git生成SSH key
使用https:// 提交GitHub上的私有项目时,需要每次都输入帐号和密码,比较麻烦. 比如我自己的笔记本,在push的时候我可不想每次都输入这些. 那就使用SSH吧,这个需要在GitHub的帐号 ...
- 比Redis更快:Berkeley DB面面观
比Redis更快:Berkeley DB面面观 Redis很火,最近大家用的多.从两年前开始,Memcached转向Redis逐渐成为潮流:而Berkeley DB可能很多朋友还很陌生,首先,我们简单 ...
- 函数(def)
一.为什么要使用函数: 面向过程的编程在代码里有很多代码是可以重复利用的,如果使用面向过程编程会使代码显得纷繁复杂,不利于他人和自己日后的阅读.而在函数式编程里,则可以把每个可重复利用的功能封装在一个 ...
- linux 查看是否安装perl模块
这里介绍两种linux中查看perl模块是否安装的方法,一种是对于单体的模块,一种是对于群体的. 单体验证: [root@root ~]# perl -MShell -e "print\&q ...
- Redis常用命令入门——列表类型(一级二级缓存技术)
获取列表片段 redis > LRANGE KEY_NAME START END lrange命令比较常用,返回从start到stop的所有元素的列表,start和stop都是从0开始. (1) ...
- 04 SQL是关于集合的
面向集合去思考 要想成为写SQL语句的高级专家, 最困难的是一个转变就是从面相过程的思维方式转变到面相集合的思维方式. 首先要停止那些一次处理一行数据的过程化步骤思维, 试着把思路转移到使用类似于 “ ...
- DDL和DML的定义和区别
DML(Data Manipulation Language)数据操纵语言: 适用范围:对数据库中的数据进行一些简单操作,如insert,delete,update,select等. DDL(Data ...
- Oracle数据库高级查询(五)集合查询
现实需求有时候需要将多个查询组合到一个查询中去 这时就需要使用集合查询操作了 这个操作类似于数学中的交集,并集,和补集的操作 交集就是返回两个查询共有的记录,关键字是INTERSECT 并集是返回 ...