Scene:
Front and back end separation, the local front-end development call interface will have cross-domain issues, there are generally the following 3 solutions:
1. Back-end interface package to run locally (disadvantage: Every backend update to test the next update package, but also to build a Java environment, trouble)
2. Cors cross-domain: backend interface When returning, add ' Access-control-allow-origin ':* in the header (sometimes the backend is not easy to handle, the front end of the egg hurts)
3. Use Nodejs to build a local HTTP server, and to determine the interface URL to be forwarded, a perfect solution to the local development of cross-domain issues.
The technology used:
1. Nodejs build Local HTTP Server
2. Apply Node-http-proxy, do the forwarding of interface URL
Specific methods:
1. Node. JS builds a local HTTP server referencing Shawn.xie's "Nodejs building local HTTP Server"
2. Node. js does forwarding using NODE-HTTP-PROXY implementation, Official document: Https://github.com/nodejitsu/node-http-proxy#using-https
3. Method of operation reference: Http://hao.jser.com/archive/10394/?utm_source=tuicool&utm_medium=referral
4. Here is my own actual operation
Project preparation
1. NPM Initialization
NPM Init
2. Installing the Node-http-proxy module
NPM Install Http-proxy--save-dev
3. Project Structure
In the following example, we put the HTML file directly in the root directory './', or you can specify a site directory that can be customized in proxy.js
Configuring HTTP server and Proxy forwarding
varPORT = 3000;varHTTP = require (' http ');varUrl=require (' url ');varFs=require (' FS ');varMine=require ('./mine ')). Types;varPath=require (' Path ');varHttpproxy = require (' Http-proxy '));varProxy =Httpproxy.createproxyserver ({target:' Http://192.168.10.38:8180/',//Interface Address //The following settings are used for HTTPS //SSL: { //key:fs.readFileSync (' server_decrypt.key ', ' UTF8 '), //cert:fs.readFileSync (' server.crt ', ' UTF8 ') // }, //Secure:false});p Roxy.on (' Error ',function(Err, req, res) {Res.writehead (500, { ' Content-type ': ' Text/plain ' }); Console.log (ERR); Res.end (' Something went wrong. And we are reporting a custom error message. ');});varServer = Http.createserver (function(Request, response) {varpathname =Url.parse (request.url). Pathname; //var realpath = path.join ("main-pages", pathname);//Specify root directory varRealpath = Path.join ("./", pathname); Console.log (pathname); Console.log (Realpath); varext =Path.extname (Realpath); Ext= ext? Ext.slice (1): ' Unknown '; //determine if the interface is accessed by proxy forwarding if(Pathname.indexof ("Mspj-mall-admin") > 0) {Proxy.web (request, response); return; } fs.exists (Realpath,function(exists) {if(!exists) {Response.writehead (404, { ' Content-type ': ' Text/plain ' }); Response.Write ("This request URL" + pathname + "is not found on this server."); Response.End (); } Else{fs.readfile (Realpath,"Binary",function(err, file) {if(Err) {Response.writehead (500, { ' Content-type ': ' Text/plain ' }); Response.End (ERR); } Else { varContentType = Mine[ext] | | "Text/plain"; Response.writehead (200, { ' Content-type ': ContentType}); Response.Write (File,"Binary"); Response.End (); } }); } });}); Server.listen (PORT); Console.log ("Server runing at Port:" + Port + ".");
MINE. Js
Here refer to the source code of Shawn.xie, supplemented by a few font file MIME.
Exports.types = { "CSS": "Text/css", "GIF": "Image/gif", "HTML": "Text/html", "ico": "Image/x-icon", "JPEG": "Image/jpeg", "JPG": "Image/jpeg", "JS": "Text/javascript", "JSON": "Application/json", "PDF": "Application/pdf", "PNG": "Image/png", "SVG": "Image/svg+xml", "SWF": "Application/x-shockwave-flash", "TIFF": "Image/tiff", "TXT": "Text/plain", "WAV": "Audio/x-wav", "WMA": "AUDIO/X-MS-WMA", "WMV": "Video/x-ms-wmv", "xml": "Text/xml", "Woff": "Application/x-woff", "WOFF2": "Application/x-woff2", "TFF": "Application/x-font-truetype", "OTF": "Application/x-font-opentype", "EoT": "Application/vnd.ms-fontobject"};
The above is the full source
Then change the interface address in the project to Http://localhost:3000/...
start the Nodejs service
Start cmd, navigate to the project directory, run
Node Proxy.js
Access:
Http://localhost:3000/index.html
You can see the http://localhost:3000/called in the project ..... will be from http://192.168.10.38:8180/... Gets the data and forwards it to local.
So there's no cross-domain.
node. js mates with Node-http-proxy to solve locally developed Ajax cross-domain issues