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:
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?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.