zoukankan      html  css  js  c++  java
  • nodeschool.io 8

    ~~ HTTP COLLECT ~~

    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 and the second line
    should contain the complete String of characters sent by the server.

    ----------------------------------------------------------------------
    HINTS:

    There are two approaches you can take to this problem:

    1) Collect data across multiple "data" events and append the results
    together prior to printing the output. Use the "end" event to
    determine when the stream is finished and you can write the output.

    2) Use a third-party package to abstract the difficulties involved in
    collecting an entire stream of data. Two different packages provide a
    useful API for solving this problem (there are likely more!):
    `bl` (Buffer List) and `concat-stream`; take your pick!

    http://npm.im/bl
    http://npm.im/concat-stream

    To install a Node package, use the Node Package Manager `npm`. Simply
    type:

    npm install bl

    And it will download and install the latest version of the package
    into a subdirectory named `node_modules`. Any package in this
    subdirectory under your main program file can be loaded with the
    `require` syntax without being prefixed by './':

    var bl = require('bl')

    Node will first look in the core modules and then in the
    `node_modules` directory where the package is located.

    If you don't have an Internet connection, simply make a `node_modules`
    directory and copy the entire directory for the package you want to
    use from inside the learnyounode installation directory:
    C:UsersdzhangAppDataRoaming pm ode_moduleslearnyounode ode_modulesl
    C:UsersdzhangAppDataRoaming pm ode_moduleslearnyounode ode_modulescon
    cat-stream

    Both `bl` and `concat-stream` can have a stream piped in to them
    and they will collect the data for you. Once the stream has ended, a
    callback will be fired with the data:

    response.pipe(bl(function (err, data) { ... }))

    Note that you will probably need to `data.toString()` to convert from
    a Buffer.

    ----------------------------------------------------------------------

    httpCollect.js

    const bl = require("bl");
    var http = require('http');
    http.get(process.argv[2], function(res) {
        res.pipe(bl(function(err,data){
            console.log(data.length);
            console.log(data.toString());
        }));
    });
  • 相关阅读:
    bzoj1007: [HNOI2008]水平可见直线(单调栈)
    1264: [AHOI2006]基因匹配Match(动态规划神题)
    bzoj1433: [ZJOI2009]假期的宿舍(最大二分图匹配)
    bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)
    [ZJOI2007]矩阵游戏
    [HAOI2007]覆盖问题
    [ZJOI2008]树的统计
    [ZJOI2010]数字计数
    [HAOI2006]旅行
    [HAOI2006]数字序列
  • 原文地址:https://www.cnblogs.com/della/p/3433341.html
Copyright © 2011-2022 走看看