Node - http-proxy

About

https://www.npmjs.com/package/http-proxy is a What is an HTTP Proxy?

Example

const { createServer } = require('http')
const httpProxy = require('http-proxy')
const { parse } = require('url')


const proxy = httpProxy.createProxyServer()
const target = 'http://localhost:3001'


createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl

switch (pathname) {

  case '/api/login.js':
	proxy.web(req, res, { target }, error => {
	  console.log('Error!', error)
	})
	break

  default:
	handle(req, res, parsedUrl)
	break
}
}).listen(3000, err => {
if (err) throw err
   console.log('> Ready on http://localhost:3000')
})





Discover More
Node - http-proxy-middleware

A proxy middelware library for Node based on http-proxy-middleware



Share this page:
Follow us:
Task Runner