Learning Node.js

ietf發表於2018-10-04

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 (
) 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("
").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==="
")
            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()+"
")
}).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])複製程式碼

相關文章