In order to simulate sending requests to the background, you need to build a demo of the web project, and use the express of nodejs to simulate the web project
var express = require("express"); var http = require("http"); var app = express (); ///////////////////// Get the data from the post////////////////////// ////// var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: false })); ///////////////////////////////////////////////////////////////// /// var ejs = require("ejs"); //Use the set method to specify values for the system variables "views" and "view engine". app.set("views", __dirname + "/views"); // Specify the suffix of the template file as html app.set('view engine', 'html'); // run the hbs module app.engine('html', ejs.__express); ////////////////////////////////////////////////////////////////////////////////////////// ///////// var router = express.Router(); var router1 = require('./routes/router1'); var router2 = require('./routes/router2'); var router3 = require('./routes/router3'); var testRouter = require('./routes/test/test'); var sqhIndex = require('./routes/sqh/sqhIndex'); var klwIndex = require('./routes/klw/klwIndex'); //Set the root directory of the web project app.use(express.static(__dirname + '/')); // allow cross domain app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1') if(req.method=="OPTIONS") res.send(200);/*Let the options request return quickly*/ else next(); }); app.use('/router1', router1); app.use('/router2', router2); app.use('/router3', router3); app.use('/test', testRouter); app.use('/sqh', sqhIndex); app.use('/klw', klwIndex); http.createServer(app).listen(3000);
Note: You need to put the request code that allows cross-domain in front of all routes , because when the request is accepted by express, the order of interception is determined according to the routing order of the code written by yourself . Once the interception conditions are met, if you do not execute next( ) method, the request will not be sent to the next route interceptor.