Learning Node.js

HELLO WORLD (Exercise 1 of 13)

Write a program that prints the text "HELLO WORLD" to the console
(stdout).

console.log("HELLO WORLD")

BABY STEPS (Exercise 2 of 13)

Write a program that accepts one or more numbers as command-line arguments
and prints the sum of those numbers to the console (stdout).

let sum = 0

process.argv.forEach((item,index) => {
    if(index>1)
        sum+=Number(item)
});
console.log(sum)

MY FIRST I/O! (Exercise 3 of 13)

Write a program that uses a single synchronous filesystem operation to
read a file and print the number of newlines (\n) 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 (i.e., process.argv[2]). You do not need to make
your own test file.

let fs = require("fs")

let data = fs.readFileSync(process.argv[2],"utf-8");
console.log(data.split("\n").length -1);

MY FIRST ASYNC I/O! (Exercise 4 of 13)

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.

var fs = require("fs")

fs.readFile(process.argv[2],"utf-8",(err,data) => {
    let newlines = 0
    if(err) console.error(err)
    for (let codepoint of data){

        if(codepoint==="\n")
            newlines++;
    }
    console.log(newlines)
});

FILTERED LS (Exercise 5 of 13)

Create a program that prints a list of files in a given directory,
filtered by the extension of the files. You will be provided a directory
name as the first argument to your program (e.g. '/path/to/dir/') and a
file extension to filter by as the second argument.

For example, if you get 'txt' as the second argument then you will need to
filter the list to only files that end with .txt. Note that the second
argument will not come prefixed with a '.'.

Keep in mind that the first arguments of your program are not the first
values of the process.argv array, as the first two values are reserved for
system info by Node.

The list of files should be printed to the console, one file per line. You
must use asynchronous I/O.

let fs = require("fs")

fs.readdir(process.argv[2], (err, list) => {
    if (err) return console.error(err)
    list.forEach((item) => {
        if (item.endsWith("." + process.argv[3]))
            console.log(item)
    })

});

MAKE IT MODULAR (Exercise 6 of 13)

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 (one file per line) to the console. You must use
asynchronous I/O.

You must write a module file to do most of the work. The module must
export a single function that takes three arguments: the directory name,
the filename extension string and a callback function, in that order. The
filename extension argument must be the same as what was passed to your
program. Don't turn it into a RegExp or prefix with "." or do anything
except pass it to your module where you can do what you need to make your
filter work.

The callback function must be called using the idiomatic node(err, data)
convention. This convention stipulates that unless there's an error, the
first argument passed to the callback will be null, and the second will be
your data. In this exercise, the data will be your filtered list of files,
as an Array. If you receive an error, e.g. from your call to
fs.readdir(), the callback must be called with the error, and only the
error, as the first argument.

You must not print directly to the console from your module file, only
from your original program.

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.

These four things are the contract that your module must follow.

  1. Export a single function that takes exactly the arguments described.
  2. Call the callback exactly once with an error or some data as described.
  3. Don't change anything else, like global variables or stdout.
  4. Handle all the errors that may occur and pass them to the callback.

The benefit of having a contract is that your module can be used by anyone
who expects this contract. So your module could be used by anyone else who
does learnyounode, or the verifier, and just work.

let fs = require("fs")

module.exports =  function foo(dir,ext,callback) {
    fs.readdir(dir, (err, data) => {
        if(err)  return callback(err)
        let list = []
        data.forEach((item) => {
            if (item.endsWith("." + ext))
                list.push(item)
        })
        callback(null,list)
    });
}

let foo = require('./module2.js');

foo(process.argv[2],process.argv[3],(err,data) =>{
    if(err)
        return console.error(err)
    data.forEach((item)=>{
        console.log(item)
    })

});

HTTP CLIENT (Exercise 7 of 13)

Write a program that performs an HTTP GET request to a URL provided to you
as the first command-line argument. Write the String contents of each
"data" event from the response to a new line on the console (stdout).

let http = require('http')

http.get(process.argv[2],(res)=>{
  res.setEncoding('utf-8')
  res.on('error',(error)=>{
      return console.error(error)
  })
  res.on("data",(data)=>{
      console.log(data)
  })
}).on("error",console.error)

HTTP COLLECT (Exercise 8 of 13)

Write a program that performs an HTTP GET request to a URL provided to you
as the first command-line argument. Collect all data from the server (not
just the first "data" event) and then write two lines to the console
(stdout).

The first line you write should just be an integer representing the number
of characters received from the server. The second line should contain the
complete String of characters sent by the server.

let http = require("http")
let concatstream = require("concat-stream")

http.get(process.argv[2], (res) => {

  res.on("error", console.error)
  res.on("data", (data) => {

  })
  res.pipe(concatstream((data) => {
      console.log(data.toString().length)
      console.log(data.toString())
  }))
})

JUGGLING ASYNC (Exercise 9 of 13)

This problem is the same as the previous problem (HTTP COLLECT) in that
you need to use http.get(). However, this time you will be provided with
three URLs as the first three command-line arguments.

You must collect the complete content provided to you by each of the URLs
and print it to the console (stdout). You don't need to print out the
length, just the data as a String; one line per URL. The catch is that you
must print them out in the same order as the URLs are provided to you as
command-line arguments.

let http = require('http')

const list = []
list.push(process.argv[2])
list.push(process.argv[3])
list.push(process.argv[4])
const data = []


function fetch(url, callback) {
    http.get(url, res => {
        let buff = []
        res.on("data", data => {
            buff += data
        })
        res.on("end", () => {
            data.push(buff.toString())
            callback()
        })
    })
}

fetch(list[0], () => {
    fetch(list[1], () => {
        fetch(list[2], () => {
            data.forEach(item => {
                console.log(item)
            })
        })
    })
})

TIME SERVER (Exercise 10 of 13)

Write a TCP time server!

Your server should listen to TCP connections on the port provided by the
first argument to your program. For each connection you must write the
current date & 24 hour time in the format:

"YYYY-MM-DD hh:mm"

followed by a newline character. Month, day, hour and minute must be
zero-filled to 2 integers. For example:

"2013-07-06 17:42"

After sending the string, close the connection.

let net = require('net')

net.createServer(socket => {
    let date = new Date()
    "x".padStart(3, "0")
    socket.end(date.getFullYear() + "-" + ((date.getMonth() + 1).toString().padStart(2, "0"))
        + "-" + (date.getDate().toString().padStart(2, "0")) + " " + date.getHours() + ":" + date.getMinutes()+"\n")
}).listen(process.argv[2])

HTTP FILE SERVER (Exercise 11 of 13)

Write an HTTP server that serves the same text file for each request it
receives.

Your server should listen on the port provided by the first argument to
your program.

You will be provided with the location of the file to serve as the second
command-line argument. You must use the fs.createReadStream() method to
stream the file contents to the response.

let http = require("http")
let fs = require("fs")
const file = process.argv[3]
console.log(file)
http.createServer((req,res) => {
    fs.createReadStream(file).pipe(res)
}).listen(process.argv[2])

HTTP UPPERCASERER (Exercise 12 of 13)

Write an HTTP server that receives only POST requests and converts
incoming POST body characters to upper-case and returns it to the client.

Your server should listen on the port provided by the first argument to
your program.

let http = require("http")
let map = require("through2-map")

http.createServer((req, res) => {
    if (req.method == "POST") {
        req.pipe(map(chunk => {
            return chunk.toString().toUpperCase()
        })).pipe(res)
    }
}).listen(process.argv[2])

HTTP JSON API SERVER (Exercise 13 of 13)

Write an HTTP server that serves JSON data when it receives a GET request
to the path '/api/parsetime'. Expect the request to contain a query string
with a key 'iso' and an ISO-format time as the value.

For example:

/api/parsetime?iso=2013-08-10T12:10:15.474Z

The JSON response should contain only 'hour', 'minute' and 'second'
properties. For example:

{
  "hour": 14,
  "minute": 23,
  "second": 15
}

Add second endpoint for the path '/api/unixtime' which accepts the same
query string but returns UNIX epoch time in milliseconds (the number of
milliseconds since 1 Jan 1970 00:00:00 UTC) under the property 'unixtime'.
For example:

{ "unixtime": 1376136615474 }

Your server should listen on the port provided by the first argument to
your program.

let http = require("http")
let url = require("url")

http.createServer((req, res) => {
    let urlObj = url.parse(req.url, true)
    let isoDate = urlObj.query.iso
    let date = new Date(isoDate)
    let result
    if (urlObj.pathname == "/api/parsetime") {
        result = res.end(JSON.stringify({
            hour: date.getHours(),
            minute: date.getMinutes(),
            second: date.getSeconds()
        }))
    }

    if (urlObj.pathname == "/api/unixtime") {
        result = res.end(JSON.stringify({
            unixtime: date.getTime(),
        }))
    }
    if (result) {
        res.writeHead(200, {'Content-Type': 'application/json'})
        res.end(result)
    }
    else {
        res.writeHead(404)
        res.end()
    }

}).listen(process.argv[2])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,013评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,205评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,370评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,168评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,153评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,954评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,271评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,916评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,382评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,877评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,989评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,624评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,209评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,199评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,418评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,401评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,700评论 2 345

推荐阅读更多精彩内容