Recently, I played nodeJS as a server, there was a cross-domain problem, and I found a lot of documents on the Internet. Basically, the following methods can be used to solve it

// I created the project using the Express tool
app.all("*".function (req,res,next) {
    res.header("Access-Control-Allow-Origin"."*");
    res.header("Access-Control-Allow-Headers"."X-Requested-With");
    res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By".'3.2.1');
    res.header("Content-Type"."application/json; charset=utf-8");
    // next();
    console.log(req.method.toLowerCase());
    if (req.method.toLowerCase() == 'options') {
        res.send(200);  // Let options try to end the request quickly
    } else{ next(); }});Copy the code

However, my front-end passed the token in the header, and I didn’t use the plug-in to resolve the cross domain, so I had to write it differently

app.all("*".function (req,res,next) {
    res.header("Access-Control-Allow-Origin"."*");
    // Add a token here
    res.header("Access-Control-Allow-Headers"."X-Requested-With,token");
    res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By".'3.2.1');
    res.header("Content-Type"."application/json; charset=utf-8");
    console.log(req.method.toLowerCase());
    if (req.method.toLowerCase() == 'options') {
        res.send(200);  // Let options try to end the request quickly
    } else{ next(); }});Copy the code