Node cross-domain access

after accessing the node server across domains, the post request becomes the options, setting request header, and the data submitted by the post request cannot be obtained

var http = require ("http");
var url = require ("url");

http.createServer(function (req, res) {
    var urlObj = url.parse(req.url);
    var pathname = urlObj.pathname;
    // nodejs  favicon.ico
    if (!pathname.indexOf("/favicon.ico")) {
        return; 
    };
     let requestM=req.method;

    res.writeHeader(200,{
        "access-control-allow-origin": "*",
        "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
        "access-control-allow-headers": "Content-Type,Content-Length, Authorization, Accept,X-Requested-With",
        "access-control-max-age": 10,
        "Content-Type": "application/json"
    });

    if(requestM==="POST"){
        var post = "";  
                // reqdatapost
                req.on("data", function(chunk){    
                    post += chunk;
                });
    }
    //post 

}).listen(3000);
Mar.07,2021

how to post data, post it and take a look at
. In addition, I don't think it is necessary to write allow header in the res writer. Do you have such high requirements?


options requests are browser behavior, because browsers (some) send an options request to test across domains, and if the request is responded, the normal request will be made later.

so all you have to do is return a 200 status code for the options request.

Menu