nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~
Write a program that uses a single asynchronous filesystem operation
to read a file and print the number of newlines it contains to the
console (stdout), similar to running `cat file | wc -l`.
The full path to the file to read will be provided as the first
command-line argument.
----------------------------------------------------------------------
HINTS:
The solution to this problem is almost the same as the previous
problem except you must now do it the Node.js way: asynchronous.
Instead of `fs.readFileSync()` you will want to use `fs.readFile()`
and instead of using the return value of this method you need to
collect the value from a callback function that you pass in as the
second argument.
Remember that idiomatic Node.js callbacks normally have the signature:
function (err, data) { ... }
so you can check if an error occurred by checking whether the first
argument is truthy. If there is no error, you should have your
`Buffer` object as the second argument. As with `readFileSync()`,
you can supply 'utf8' as the second argument and put the callback as
the third argument and you will get a `String` instead of a `Buffer`.
Documentation on the `fs` module can be found by pointing your browser
here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\fs.h
tml
var fs = require('fs');
fs.readFile(process.argv[2],"utf-8",function(err,data){
if(err) throw err;
console.log(data.split("\n").length-1);
});
nodeschool.io 4的更多相关文章
- 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 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- 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 ...
随机推荐
- Java——Image 图片合并
1.合并图片 package com.tb.image; import java.awt.Image; import java.awt.image.BufferedImage; import java ...
- OnClientClick和OnClick同时使用!
摘自:http://www.cnblogs.com/zhuiyi/archive/2011/07/04/2097804.html 其实有的时候需要客户端和服务端双重校验,为什么这么说呢? 比果说,一个 ...
- Quick-Cocos2d-x v3.3 异步加载Spine方案 转
Quick-Cocos2d-x v3.3 异步加载Spine方案 浩月难求也与2015-03-25 15:06:3441 次阅读 背景 项目中使用了Quick-Cocos2d-x 3.3,由于Spin ...
- java运行内存分配图(转)
Java的内存分配 Java程序运行时的内存结构分成:方法区.栈内存.堆内存.本地方法栈几种. 方法区 存放装载的类数据信息,包括:基本信息:每个类的全限定名.每个类的直接超类的全限定 ...
- [转载] 深入 superviser
模块简介 新人接触线上的时候一般都会碰到supervise这个工具,导师对这个模块的解释一般就是,这个模块是监控进程,当进程挂掉之后,supervise会将进程启动.这样当进程出 现问题,如因出cor ...
- Python中的join()函数split()函数
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- (三)主控板生级uboot与内核
一.升级uboot 把我的电脑增加一个192.168.1.3的ip,方便升级boot. 1.连接串口,连接网口到GE1; 2.登陆电脑后到D:\MTG3000MAIN\first目录打开tftp32 ...
- 【Todo】Nginx架构学习
要进行Web服务,绕不开的就是Nginx.这已经是大型网站的标配.对Nginx进行一定程度的深入学习. http://www.ituring.com.cn/article/4436 http://bl ...
- 【Todo】单例模式各种实现方式及并发安全
Java 40道面试题不错:http://www.tuicool.com/articles/VRVFZb 其中有一道题目: 单例模式的线程安全性 老生常谈的问题了,首先要说的是单例模式的线程安全意味着 ...
- 转:如何学习SQL(第一部分:SQL基础)
转自:http://blog.163.com/mig3719@126/blog/static/285720652010950712271/ 1. 为什么学习SQL 自人类社会形成之日起,社会的运转就在 ...