conclusion
The forward proxy hides the real client and the reverse proxy hides the real server
- Forward agent
- The reverse proxy
- use
http-proxy-middware
Implement forward proxy for cross-domain purposes
const http = require('http') const proxy = require('http-proxy-middleware') http.createServer((req, res) => { let url = req.url res.writeHead(200, { 'Access-Control-Allow-Origin': '*'})// Use CORS to solve the front-end page cross-domain to the 8080port /* 1, API: is a code for proxy 2, pathRewrite: cancel API path, implement the function: none pathRewrite: Localhost :8080/ API /path ==>m.lagou.com/api/path pathRewrite: localhost:8080/api/path ==>m.lagou.com/path */ if (/^/api/.test(url)) { let apiProxy = proxy('/api', { target: 'https://m.lagou.com', changeOrigin: true, pathRewrite: { '^/api': ApiProxy (req, res)} else {switch (url) {case '/index.html': res.end('index.html') break case '/search.html': res.end('search.html') break default: res.end('[404]page not found.') } } }).listen(8080)Copy the code