expressJS NodeJS

NodeJS proxy router streams implementation example for ExpressJS

Delivering proxied GET response from ExpressJS server to client requesting data from url with query param.

curl http://host?url=https://yourUrl.com

Route for proxy – Pipe data to response stream

  • Not a standalone file, implemented as route in express with app.use()
  • var Router = require('express').Router()
    var https = require('https')
    
    Router.get('/', function (req, res, next) {
    
        https.get(req.query.url, (data) => {
    
            //remove express headers
            Object.keys(res.getHeaders()).forEach((headerName) => {
                res.removeHeader(headerName)
            })
    
            // write status
            res.status = data.statusCode
            // write headers
            Object.keys(data.headers).forEach((headerName) => {
                res.setHeader(headerName, data.headers[headerName])
            })
    
             //Pipe data to response stream
             data.pipe(res)
    
        })
    
    })
    
    module.exports=Router
    

    Route for proxy – Transform data and pipe to response stream

    • Not a standalone file, implemented as route in express with app.use()
    var router = require('express').Router()
    var https = require('https')
    // create and prepare stream
    var {Readable} = require('stream')
    var readStream = Readable()
    readStream._read = function () {}
    
    router.get('/', function(req, res, next) {
    
      https.get(req.query.url, (data) => {
    
        //remove express headers
        Object.keys(res.getHeaders()).forEach((headerName) => {
          res.removeHeader(headerName)
        })
    
        // write status
        res.status = data.statusCode
        // write headers
        Object.keys(data.headers).forEach((headerName) => {
          res.setHeader(headerName, data.headers[headerName])
        })
    
        data.on('data', (chunk) => {
    
          // transform data here 
          var i = chunk.toString('utf8')
    
          // UpperCase all letters
          readStream.push(i.toUpperCase())
    
          console.log(readStream.readableFlowing === true)
    
        }).on('end', () => {
    
          // readableStream does not invoke automatic end, whereas data.pipe(res) would do that
          readStream.unpipe(res)
          readStream.destroy()
          console.log('is still flowing?', readStream.readableFlowing === true)
          console.log('is destroyed', readStream.destroyed === true)
          return res.end()
        })
    
        readStream.pipe(res, {
          end: true
        })
        
      })
    
    
    })
    
    module.exports = router
    

    0 comments on “NodeJS proxy router streams implementation example for ExpressJS

    Vastaa

    Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:

    WordPress.com-logo

    Olet kommentoimassa WordPress.com -tilin nimissä. Log Out /  Muuta )

    Google photo

    Olet kommentoimassa Google -tilin nimissä. Log Out /  Muuta )

    Twitter-kuva

    Olet kommentoimassa Twitter -tilin nimissä. Log Out /  Muuta )

    Facebook-kuva

    Olet kommentoimassa Facebook -tilin nimissä. Log Out /  Muuta )

    Muodostetaan yhteyttä palveluun %s

    %d bloggaajaa tykkää tästä: