DEV Community

Cover image for Pipe Stream to Express
Adam K Dean
Adam K Dean

Posted on • Edited on

1 1

Pipe Stream to Express

Originally posted on November 13th, 2017 (more info)

As part of a project I'm working on, I need to grab some data via HTTP/S and transmit it as binary, but with access to it's headers. The following is a quick proof of concept to listen for HTTP requests with Express, request an external image on request, and pipe the response back through to the Express response socket while having access to response metadata such as headers.

'use strict'

const express = require('express')
const request = require('request')
const through2 = require('through2')

const app = express()
const imageUrl = 'http://via.placeholder.com/800x600?text=example'

app.use((incomingRequest, outgoingResponse) => {
  const outgoingRequest = request(imageUrl)
  const bufferOnPipe = through2(function (chunk, enc, callback) {
    this.push(chunk)
    callback()
  })
  const bufferedResponse = outgoingRequest.pipe(bufferOnPipe)

  outgoingRequest.on('response', (incomingResponse) => {
    if (incomingResponse.statusCode === 200) {
      console.log('statusCode:', incomingResponse.statusCode)
      console.log('headers:', incomingResponse.headers)
      bufferedResponse.pipe(outgoingResponse)
    } else {
      console.log('non-200 statusCode:', incomingResponse.statusCode)
    }
  })
})

app.listen(8000, () => {
  console.log('listening on 8000')
})
Enter fullscreen mode Exit fullscreen mode

Next post in series: Pass Request buffer through Redis

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more