Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question upload file after proxy #473

Open
Irenedan opened this issue Dec 4, 2020 · 5 comments
Open

Question upload file after proxy #473

Irenedan opened this issue Dec 4, 2020 · 5 comments

Comments

@Irenedan
Copy link

Irenedan commented Dec 4, 2020

I try to use the form-data in the body to upload file, but it looks like that I can't get my req information after proxy

@Irenedan
Copy link
Author

Irenedan commented Dec 9, 2020

@monkpow Hello, Does express-http-proxy support multipart form data and file uploads? Is anyone working on it?

@microMerchine
Copy link

@Irenedan fixed it ?

@bytadaniel
Copy link

Hi there! Yeah, this really need to be fixed

@bytadaniel
Copy link

bytadaniel commented Mar 5, 2023

@microMerchine @Irenedan I have found out how to fix this.
Thanks to btmurrel in this issue #127

import dotenv from 'dotenv'
import cors from 'cors'
import default_proxy from 'express-http-proxy'
import express, { NextFunction, Request, Response } from 'express'

dotenv.config()

const app = express()

app.use((req, _res, next) => next(console.log(req.method, req.url)))
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

const isMultipartRequest = function (req: Request) {
  const contentTypeHeader = req.headers['content-type']
  return contentTypeHeader && contentTypeHeader.indexOf('multipart') > -1
}

const proxy = function (host: string) {
  return function (req: Request, res: Response, next: NextFunction) {
    let reqBodyEncoding
    let reqAsBuffer = false
    let parseReqBody = true

    if (isMultipartRequest(req)) {
      reqAsBuffer = true
      reqBodyEncoding = null
      parseReqBody = false
    }
    return default_proxy(host, {
      reqAsBuffer,
      reqBodyEncoding,
      parseReqBody
    })(req, res, next)
  }
}

app.all(/widget/, proxy('localhost:3002'))
app.all('*', proxy('localhost:3001'))

app.get('/healthcheck', (_req, res) => res.sendStatus(200))

app.listen(process.env.PORT, () => console.log(`Listen -> http://localhost:${process.env.PORT}`))

@Xavier577
Copy link

@microMerchine @Irenedan I have found out how to fix this. Thanks to btmurrel in this issue #127

import dotenv from 'dotenv'
import cors from 'cors'
import default_proxy from 'express-http-proxy'
import express, { NextFunction, Request, Response } from 'express'

dotenv.config()

const app = express()

app.use((req, _res, next) => next(console.log(req.method, req.url)))
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

const isMultipartRequest = function (req: Request) {
  const contentTypeHeader = req.headers['content-type']
  return contentTypeHeader && contentTypeHeader.indexOf('multipart') > -1
}

const proxy = function (host: string) {
  return function (req: Request, res: Response, next: NextFunction) {
    let reqBodyEncoding
    let reqAsBuffer = false
    let parseReqBody = true

    if (isMultipartRequest(req)) {
      reqAsBuffer = true
      reqBodyEncoding = null
      parseReqBody = false
    }
    return default_proxy(host, {
      reqAsBuffer,
      reqBodyEncoding,
      parseReqBody
    })(req, res, next)
  }
}

app.all(/widget/, proxy('localhost:3002'))
app.all('*', proxy('localhost:3001'))

app.get('/healthcheck', (_req, res) => res.sendStatus(200))

app.listen(process.env.PORT, () => console.log(`Listen -> http://localhost:${process.env.PORT}`))

Thank you very much for this helped me fixed my issues without moving away from express-http-proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants