scene:
The front-end and back-end separation, and the local front-end development and calling interface will have cross-domain problems. There are generally three solutions:
1. Package the backend interface to run locally (disadvantages: every time the backend update is updated, you have to go to the next update package for the test server, and you also have to build a Java running environment locally, which is troublesome)
2. CORS cross-domain: When the backend interface returns, add 'Access-Control-Allow-origin':* to the header (sometimes the backend is inconvenient to handle this, and the frontend will be painful)
3. Use nodejs to build a local http server and forward it when accessing the interface URL, perfectly solve the cross-domain problem during local development.
Techniques used:
1. Build a local http server with nodejs
2. Apply node-http-proxy to forward interface urls
Specific methods:
1. node.js builds a local http server. Refer to shawn.xie's "nodejs builds a local http server"
2. Node.js is used to forward node-http-proxy. The official document is: https://github.com/nodejitsu/node-http-proxy#using-https
3. The operation method is referenced: http://hao.jser.com/archive/10394/?utm_source=tuicool&utm_medium=referral
4. Here are my own practical operations
Project preparation
1. npm initialization
npm init
2. Install 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 specify a website directory, which can be customized in proxy.js
Configure HTTP server and PROXY forwarding
var PORT = 3000;var http = require('http');var url=require('url');var fs=require('fs');var mine=require('./mine').types;var path=require('path');var httpProxy = require('http-proxy');var proxy = 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});proxy.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.');});var server = http.createServer(function (request, response) {var pathname = url.parse(request.url).pathname;//var realPath = path.join("main-pages", pathname); // Specify the root directory var realPath = path.join("./", pathname);console.log(pathname);console.log(realPath);var ext = path.extname(realPath);ext = ext ? ext.slice(1): 'unknown';//Judge if it is an interface access, forward 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 + " was 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 {var contentType = mine[ext] || "text/plain";response.writeHead(200, {'Content-Type': contentType});response.write(file, "binary");response.end();}});}});});server.listen(PORT);console.log("Server running at port: " + PORT + ".");MINE.JS
Here we refer to the source code of shawn.xie and add a few font files 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 all the source code
Then change the interface address in the project to http://localhost:3000/...
Start nodejs service
Start cmd, locate the project directory, and run
node proxy.js
access:
http://localhost:3000/index.html
You can see that the data will be obtained from http://localhost:3000/...... in the project, and then forwarded to the local area.
This way there is no cross-domain.
The above is the editor’s introduction to Node.js and node-http-proxy to solve the cross-domain problem of local development ajax. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!