~~ 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. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  2. Android 获取存储空间

    package com.example.getMem; import java.io.File; import android.os.Build;import android.os.Bundle;im ...

  3. ProgressBar 的使用

    ProgressBar 的使用方法 转载:http://blog.csdn.net/mad1989/article/details/38042875

  4. Apply Root Motion

    Apply Root Motion 应用根动作: Should we control the character's position from the animation itself or fro ...

  5. require或include相对路径多层嵌套引发的问题

    require或include相对路径多层嵌套引发的问题   php中require/include 包含相对路径的解决办法 在PHP中require,include一个文件时,大都是用相对路径,是个 ...

  6. c#扩展方法的理解(二:接口)

    namespace ExtensionInterfaceMethod { class Program { static void Main(string[] args) { //使用接口变量来调用扩展 ...

  7. oracle命令识记

    连接数据库 sqlplus /nolog; conn / as sysdba; set ORACLE_SID=实例名; 查看表结构命令 select table_name from user_tabl ...

  8. ServiceStack.OrmLite 笔记10-group having 分页orderby等

    group having 分页等 var ev = OrmLiteConfig.DialectProvider.SqlExpression(); group的使用 同sql一样,注意group分组的字 ...

  9. 预习 jQuary

    一.jQuary简介 1.jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTM ...

  10. JQery 中的 $(".bb:eq(1)") eq () 解释。。

    这是一段代码: <div id="bb">a</div> <div id="bb">b</div> <di ...