~~ 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. Linux用户应知应会的7个‘ls’命令的独特技巧

    在前面我们系列报道的两篇文章中,我们已经涵盖了关于‘ls’命令的绝大多数内容.本文时‘ls命令’系列的最后一部分.如果你还没有读过该系列的其它两篇文章,你可以访问下面的链接. Linux中的15个基本 ...

  2. BZOJ 3752 世界树

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3572 题意:给出一棵树.若干询问.每个询问给出m个点,输出每个点管辖的点的个数.x ...

  3. ios照片获取,拍照功能

    // //  HYBPhotoPickerManager.h //  ehui // //  Created by 黄仪标 on 14/11/26. //  Copyright (c) 2014年 黄 ...

  4. 如何解决Angular 2 的templateUrl和styleUrl的路径问题?

    参考地址:https://github.com/kittencup/angular2-ama-cn/issues/18 前言: templateUrl表示的是组件在浏览器中运行时依赖的模板地址,所以在 ...

  5. Android中利用SharedPreferences保存信息

    package com.example.sharepreferen; import android.content.Context; import android.content.SharedPref ...

  6. 强制性签出被人没有签入的文件(在.net开发vs中)

    灵感,是天才的女神.她并不步履蹒跚地走过,而是在空中像乌鸦那么警觉地飞过的,她没有什么剽带给诗人抓握,她的头是一团烈火,她溜得快,像那些白里带红的鹤,教猎人见了无可奈何.——巴尔扎克(上海网站建设) ...

  7. mysql概要(十二)事务

    1.特性 2.开启结束取消事务:需要选择支持事务的引擎 3,隐式提交事务: 4.事务的隔离级别:

  8. maven概念

     1. 下载并解压Maven:Maven下载页2. 将环境变量M2_HOME设置为解压后的目录: 3. 将M2环境变量设置为M2_HOME/bin(在Windows上是%M2_HOME%/bin,在U ...

  9. Spring AOP执行方法

      execution(* springinaction.springidol.Instrument.play(..)) * 代表返回为任意类型 springinaction.springidol.I ...

  10. ubuntu12.04_命令

    1. 切换 终端方式 与 图形界面方式: 切换到 终端方式:ctrl+alt+F1~F6(貌似有时 alt+F1~F6也行?) 切换回 图形界面方式:ctrl+alt+F7 2. ubuntu12.0 ...