~~ 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的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 2

    ~~  BABY STEPS  ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...

  4. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  5. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  6. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  7. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  8. nodeschool.io 5

    ~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...

  9. NODESCHOOL

    来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...

随机推荐

  1. SD卡驱动分析(一)

    Android下的SD卡驱动与标准LINUX下的SD卡驱动好像没有太大的区别,这里就以高通的ANDROID 2.3以代表,来简要分析一下LINUX下SD卡驱动的写法.由于小弟的技术有限,分析的有错的地 ...

  2. jquery之 off()方法

    off()函数用于移除元素上绑定的一个或多个事件的事件处理函数. off()函数主要用于解除由on()函数绑定的事件处理函数. 该函数属于jQuery对象(实例). 语法 jQuery 1.7 新增该 ...

  3. ubuntu Linux 测试PHP却提示下载文件的解决办法

    ubuntu Linux 测试PHP却提示下载文件的解决办法   一般这种情况都是在刚刚开始配置环境时出现的, 输入 sudo a2enmod php5  看提示如果出现“$ This module ...

  4. 梯度下降法VS随机梯度下降法 (Python的实现)

    # -*- coding: cp936 -*- import numpy as np from scipy import stats import matplotlib.pyplot as plt # ...

  5. 线程入门之yield

    package com.thread; /** * <yiedl:把cpu让给其他线程> * <功能详细描述> * * @author 95Yang */ public cla ...

  6. 【BZOJ】4636: 蒟蒻的数列

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 145  Solved: 71[Submit][Status][Discuss] ...

  7. Mac : 强大的截图

    来源:http://irising.me/2011/11/12135/ Mac的截图功能扩展功能很强大的,不要用QQ那个COM+Ctrl+A弱爆了的截图了~ 首先说一下两种截图1.Command+sh ...

  8. 移动端 移动web屏幕适配方案 随不同宽度的屏幕而改变

    链接地址1:http://www.cnblogs.com/zjzhome/p/4802157.html 链接地址2:http://www.html-js.com/article/Mobile-term ...

  9. Thinkphp 3.2 添加 验证码 如何添加。

    1,在home模块indexController.class.php中,加入以下代码 <?php namespace Home\Controller; use Think\Controller; ...

  10. adb_常用命令

    1. adb push  电脑中的文件(包含路径)  Android中的绝对路径 2. adb pull  Android中的绝对路径文件  电脑中的绝对路径 3. adb install ??.ap ...