ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스트림
    카테고리 없음 2020. 2. 21. 02:28

     

     

    https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding

     

    ashtuchkin/iconv-lite

    Convert character encodings in pure javascript. Contribute to ashtuchkin/iconv-lite development by creating an account on GitHub.

    github.com

    Streaming

    You can stream any response to a file stream.

     

    request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))

     

    You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types (in this case application/json) and use the proper content-type in the PUT request (if the headers don’t already provide one).

     

    fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))

    Request can also pipe to itself. When doing so, content-type and content-length are preserved in the PUT headers.

     

    request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))

     

    Request emits a "response" event when a response is received. The response argument will be an instance of http.IncomingMessage.

     

    request .get('http://google.com/img.png') .on('response', function(response) {
        console.log(response.statusCode) // 200
         console.log(response.headers['content-type']) // 'image/png'
    }) .pipe(request.put('http://mysite.com/img.png'))

    To easily handle errors when streaming requests, listen to the error event before piping:

     

    request .get('http://mysite.com/doodle.png') .on('error', function(err) {
        console.error(err)
    }) .pipe(fs.createWriteStream('doodle.png'))

     

    Now let’s get fancy.

     

    http.createServer(function (req, resp) {
        if (req.url === '/doodle.png') {
            if (req.method === 'PUT') {
                req.pipe(request.put('http://mysite.com/doodle.png'))
            } else if (req.method === 'GET' || req.method === 'HEAD') {
                request.get('http://mysite.com/doodle.png').pipe(resp)
            }
        }
    })

     

    You can also pipe() from http.ServerRequest instances, as well as to http.ServerResponse instances. The HTTP method, headers, and entity-body data will be sent. Which means that, if you don't really care about security, you can do:

     

    http.createServer(function (req, resp) {
        if (req.url === '/doodle.png') {
            const x = request('http://mysite.com/doodle.png')
            req.pipe(x) x.pipe(resp)
        }
    })

     

    And since pipe() returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)

    req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)

    Also, none of this new functionality conflicts with requests previous features, it just expands them.

     

    const r = request.defaults({'proxy':'http://localproxy.com'})
    http.createServer(function (req, resp) {
        if (req.url === '/doodle.png') {
            r.get('http://google.com/doodle.png').pipe(resp)
        }
    })

     

    You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.

     

     

    댓글

Designed by Tistory.