美文网首页
用express实现CORS跨域

用express实现CORS跨域

作者: 小二儿上酒 | 来源:发表于2019-03-12 09:20 被阅读0次

    跨域请求头 cors

       // 跨域请求头 cors
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, Accept,token");
        res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        res.setHeader("X-Powered-By",' 3.2.1');
    

    express 跨域请求

    var express=require('express');
    var url=require('url');
    var app=express();
    var allowCrossDomain = function(req, res, next) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        res.header('Access-Control-Allow-Headers', 'Content-Type');
        res.header('Access-Control-Allow-Credentials','true');
        next();
    };
    app.use(allowCrossDomain);
    app.get('/apis',function (req,res,next) {
        var queryValue=url.parse(req.url).query;
        if(queryValue==='fortunewheel@sina.com'){
            res.send(true);
        }else {
            res.send(false);
        }
    
    });
    app.listen(3000);
    

    相关文章

      网友评论

          本文标题:用express实现CORS跨域

          本文链接:https://www.haomeiwen.com/subject/opawpqtx.html