Recently in the research of map related things, when the map service of the map of heaven and earth was called, a cross-domain problem was reported, so I wrote a simplest node server to solve it.

Destination address :”t0.tianditu.com/img\_w/ XXX “…

The server address: “http://192.168.1.123:9001” (to use the machine IP address)

Start by configuring the proxy in WebPack

This is the Proxy property in devServer

Parameters for

{"/API ": {target:" http://192.168.1.123:9001 ", / / interface domain secure: false, / / if it is HTTPS interface, need to configure the parameters changeOrigin: True // If the interface is cross-domain, this parameter needs to be configured}}Copy the code

Here, the target uses the local IP address, because the project startup address and server are both localhost will be error, interested partners can try.

Then place the target address in the project “t0.tianditu.com/img\_w/ XXX “…

“/api/t0.tianditu.com/img_w/xxx”

I’ll go straight to the code on the server

const http = require("http"); const { createProxyMiddleware } = require("http-proxy-middleware"); const server = http.createServer((req, res) => { let urlStr = req.url; if (/\/api/.test(urlStr)) { const proxy = createProxyMiddleware("/api", { target: "http://t0.tianditu.com", changeOrigin: true, pathRewrite: { "^/api": "", }, }); proxy(req, res); }}); server.listen(9001);Copy the code

Note: Http-proxy-Middleware should be installed through NPM first

If you find it helpful, you can support it by clicking a “like”