1

TLDR: how do I customize the "Error occurred while trying to proxy: localhost:3000/" in http-proxy-middleware?

I'm using the node package http-proxy-middleware as a proxy.

I have some patterns where I know that the proxying will fail. Here is a minimal example of what happens:

const { createProxyMiddleware } = require('http-proxy-middleware');

require('express')().use(
  '/',
  createProxyMiddleware({
    target: 'http://failhtis/'
  })
).listen(3000);

This will display this in my browser :

enter image description here

And in my console :

[HPM] Error occurred while proxying request localhost:3000/ to http://failhtis/ [ENOTFOUND] (https://nodejs.org/api/errors.html#errors_common_system_errors)

What I would love to do is generate a custom message / html for the error. How do I manage that?

I've tried the ejectPlugin method described here : https://github.com/chimurai/http-proxy-middleware#ejectplugins-boolean-default-false but without success.

To sum up: I know error will happen in my setup, how do I send a custom answer instead of the "Error occured while proxying..." message?

1 Answer 1

3

I just found the answer to my question.

Here it is for v2

const { createProxyMiddleware } = require('http-proxy-middleware');

console.log(Options)

require('express')().use(
  '/',
  createProxyMiddleware({
    target: 'http://sc_rstudio/',
    changeOrigin: true,
    logLevel: 'debug',
    onError: (err, req, res) => {
      console.log("pouet")
      res.send('<h1>Something went wrong.</h1>');
    },
  })
).listen(3000);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.