http-proxy-middleware option.onProxyReq:

'use strict';

var express = require('express');
var router = express.Router();

var proxy_filter = function (path, req) {
    return path.match('^/docs') && ( req.method === 'GET' || req.method === 'POST' );
};

var proxy_options = {
    target: 'http://localhost:8080',
    pathRewrite: {
        '^/docs' : '/java/rep/server1' // Host path & target path conversion
    },
    onError(err, req, res) {
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('Something went wrong. And we are reporting a custom error message.' + err);
    },
    onProxyReq(proxyReq, req, res) {
        if ( req.method == "POST" && req.body ) {
            // Add req.body logic here if needed....

           // ....

            // Remove body-parser body object from the request
            if ( req.body ) delete req.body;

            // Make any needed POST parameter changes
            let body = new Object();

            body.filename = 'reports/statistics/summary_2016.pdf';
            body.routeid = 's003b012d002';
            body.authid = 'bac02c1d-258a-4177-9da6-862580154960';

            // URI encode JSON object
            body = Object.keys( body ).map(function( key ) {
                return encodeURIComponent( key ) + '=' + encodeURIComponent( body[ key ])
            }).join('&');

            // Update header
            proxyReq.setHeader( 'content-type', 'application/x-www-form-urlencoded' );
            proxyReq.setHeader( 'content-length', body.length );

            // Write out body changes to the proxyReq stream
            proxyReq.write( body );
            proxyReq.end();
        }
    }
};

// Proxy configuration
var proxy = require( 'http-proxy-middleware' )( proxy_filter, proxy_options );

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Node.js Express Proxy Test' });
});

router.all('/document', proxy );

module.exports = router;

4.25
4

                                    // TIPS AND TRICK CUSTOM PROXY IN WEBPACK DEV SERVER BY restuwahyu13
//NOTE this method like using CRA in React

// manual proxy before
module.exports = {
	devServer: {
		open: true,
		compress: true,
		hot: true,
		inline: true,
		lazy: true,
		contentBase: resolve(process.cwd(), 'build'),
		port: process.env.PORT || 3000,
		proxy: {
          '/api/*', {
          	target: 'http://localhost:3001',
			secure: false,
			changeOrigin: true,
			headers: {
				'Access-Control-Allow-Origin': '*',
				'Access-Control-Allow-Credentials': '*',
				'Access-Control-Allow-Methods': '*'
			}
          }
        }
		liveReload: false
	}
}

// custom proxy after
module.exports = {
	devServer: {
    open: true,
    compress: true,
    hot: true,
    inline: true,
    watchContentBase: true,
    contentBase: resolve(process.cwd(), 'build'),
    historyApiFallback: true,
    before: (app, server, compiler) => {
      const fileExist = existsSync('./src/setupProxy.js')
      if (fileExist) {
        const pathProxy = resolve(process.cwd(), 'src/setupProxy')
        return require(`${pathProxy}`)(app)
      }
    },
    port: process.env.PORT || 3000,
    liveReload: false
  }
}

4.25 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
adding custom middleware before http-proxy-middleware middleware before http-proxy-middleware request http-proxy-middleware custom call in http-proxy-middleware http proxy middleware ws http proxy middelware http-proxy-middleware http to https example http-proxy-middleware react nodejs nextjs http proxy middleware http proxy middleware pathRewrite using http proxy middleware to change request body using http proxy middleware proxy middleware npm http-proxy-middleware dev server http-proxy-middleware get request body http-proxy-middleware next.js http-proxy-middleware example nextjs HTTP Proxy middleware available in API Middleware provided by Next.js http-proxy-middleware filter http-proxy-middleware nextjs http proxy middleware npm what is connect proxy middleware http-proxy-middleware proxy with headers http-proxy-middleware does not proxy headers http-proxy-middleware cannot proxy http-proxy-middleware js custom response reactjs http-proxy-middleware http proxy middle ware with post example http proxy middle ware proxy middleware express http proxy middleware with reaact how to proxy vs middleware proxy and middleware http-proxy-middleware using .env react http-proxy-middleware using .env http proxy middleware changeorigin http-proxy-middleware keep header http proxy middleware nextjs react http-proxy-middleware npm i http-proxy-middleware using http-proxy-middleware in react react express http-proxy-middleware require('http-proxy-middleware'); http-proxy-middleware nginx http-proxy-middleware to nginx express js proxy middleware http-proxy-middleware not working in production http-proxy-middleware react build http-proxy-middleware in react js on production js create proxy middleware http middleware proxy xfwd how to set http-proxy-middleware http-proxy-middleware post what is proxy middleware express proxy middleware\ create proxy middleware with https proxy endpoint http-proxy-middleware http-proxy-middleware change request path http-proxy-middleware change request body http proxy middleware ts http-proxy-middleware setupproxy.js http-proxy-middleware in react app http-proxy-middleware na servere hosting http-proxy-middleware Http-proxy-middleware production http-proxy-middleware in production php http-proxy-middleware in production Http-proxy-middleware https example http-proxy-middleware express example express http proxy middleware react proxy setup proxy middleware http-proxy-middleware websocket react setup proxy http-middleware-proxy http-proxy-middleware install express http-proxy-middleware create proxy middleware to different frontend http-proxy-middleware not working post create proxy middleware tutorial http proxy middleare proxy middleware react proxy middleware explained why do i need proxy middleware node http proxy middleware production node http proxy middleware next-http-proxy-middleware example http-proxy-middleware onerror proxy middleware node proxy middle ware node http-proxy-middleware react js http-proxy-middleware typescript http-proxy-middleware react in build http-proxy-middleware You need to enable JavaScript to run this app. proxy whole website http proxy middleware proxy to run middleware react use http-proxy-middleware replacement middleware for http-proxy-middleware http proxy middleware websicket http proxy middleware path http proxy middleware websocket http proxy middleware api http-proxy-middleware modify params http-proxy-middleware add params http-proxy-middleware using nextjs express server create proxy middleware how to use create proxy middleware http proxy middleware for server and client sides http-proxy-middleware for nextjs http-proxy-middleware host http-proxy-middleware subdomain http proxy middleware ssr http proxy middleware server side npm http-proxy-middleware npm http proxy middleware http-proxy-middleware add header http-proxy-middleware performance http proxy middleware npm http-proxy-middleware change status httos proxy middleware proxy middleware how to do middleware proxy only client express createProxyMiddleware http-proxy-middleware forward headers http proxy middleware for next js const { createProxyMiddleware } = require('http-proxy-middleware') http-proxy-middleware console react js http-proxy-middleware' print in console react js http-proxy-middleware react example github http-proxy-middleware react example http-proxy-middleware hide ip http-proxy-middleware react http proxy middleware double api call yarn proxy and router http proxy middleware typescript http-proxy-middleware websocket example http-proxy-middleware cache json HTTP proxy list node js http-proxy-middleware example http-proxy-middleware example express http-proxy-middleware api request reat https proxy middleware Error: Request failed with status code 404 Error: Request failed with status code 404 after using http-proxy-middleware https proxy middleware react http-proxy-middleware takes long time http-proxy-middleware npm proxy changeOrigin: false middleware proxy node express middleware proxy http-proxy-middleware change header createproxymiddleware example createproxymiddleware port proxy middleware node js http-proxy-middleware default timeout http-proxy-middleware x-forwarded host http-proxy-middleware css create proxy middleware express reverse proxy middleware alter the path after proxying with createProxyMiddleware putting alias in http-proxy-middleware http-proxy-middleware option.onProxyReq: express middleware for proxy express proxy middleware http-proxy-middleware Proxy Request http-proxy-middleware express Proxy Request http-proxy-middleware express onProxy Requeszt http-proxy-middleware express get http-proxy-middleware app.get http-proxy-middleware with express app.use(path, createProxyMiddleware(config)); http-proxy-middleware https how to get proxy pared url in http proxy-middleware pass data between services middleware proxy express proxy pathRewrite node proxy pathRewrite http-proxy-middleware node express example redirect assets http-proxy pathRewrite: { '^/api': '' } } express nginx http-proxy-middleware oauth how to disable changeOrigin http proxy middleware client side http middleware proxy is http proxy middleware secure? createproxymiddleware remove url http-proxy-middleware react setup http proxy middleware react how to use http-proxy-middleware in react express proxy-middleware react proxy middleware proxy([ http proxy middleware not display info npm proxy middleware http proxy middleware
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source