proxy 代理本地开发跨域

1,537 阅读2分钟

一:安装与加载命令

1:下载node.js

npm init 初始化工程

2:npm install  安装nodeModel包括,

3:npm install --save-dev express-http-proxy

3-1:npm install express

二:准备好proxy.js(配置好端口和域名)

参考路径:blog.csdn.net/qq149898227… 本地跨域

let express = require('express')
var proxy = require('express-http-proxy');

let app = express()


// 浏览器与服务器有同源策略限制,服务器与服务器之间没有同源策略限制
// 下面解决同源策略跨域问题,因为当前端口为3001,而前端请求端口为3000
app.all('*', function(req, res, next) { //必须卸载app.get前面才有效
    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);
        /*让options请求快速返回*/
    } else {
        next();
    }
});

// ‘proxy’表示所匹配到的url以 /proxy开头的都会由这里的中间层转发
// 'http://127.0.0.1:3001/proxy/api/search'  前端请求pathname以/proxy开头所以会被这个中间层转发到目标url

app.use('/', proxy(
    'https://s.sfddj.com', // 目标域名,只能写域名,不能带上pathname
    {
        proxyReqPathResolver: function(request) {
            console.log(request.baseUrl) // '/proxy'
            console.log(request.url) // ' /api/search'
            return request.url // 目标数据的pathname,必须有
        },
    }
));


let server = app.listen(3001, function() {
    var port = server.address().port;
    console.log('服务开启成功端口号是:' + port)
})

三:demo代码配置好.

<!--
 * @Author: your name
 * @Date: 2020-01-03 17:17:34
 * @LastEditTime : 2020-01-03 17:30:52
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \fx\deom.html
 -->
<!DOCTYPE html>
<html>

<head>
    <title></title>
</head>

<body>

</body>
<script type="text/javascript" src="jquery-1.11.0.js"></script>
<script type="text/javascript">
    $(function(argument) {
        let host_name = 'http://127.0.0.1:3001';
        console.log('')
        $.ajax({
            url: host_name + '/fxfront/interface/gethotregions?appid=APPID',
            type: 'post',
            data: {},
            sucess: function(res) {
                console.log(res);
            }
        })
    })
</script>

</html>

运行 node proxy.js运行

image.png

浏览器打开

image.png

phpStudy:虚拟机搭建****

【有利于手机调试开发,前提手机wifi和电脑同一个局域网】 由于上面的127.0.0.1:3001 是本地开发,为了方便手机调试 前提手机wifi和电脑同一个局域网 ,把这个本地的改成当前电脑的Ip.

vhosts文件****

<VirtualHost *:80>

 DocumentRoot "E:\phpStudy\WWW\fx"

 ServerName fx.com

Hosts文件****


#

# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

#

# This file contains the mappings of IP addresses to host names. Each

# entry should be kept on an individual line. The IP address should

# be placed in the first column followed by the corresponding host name.

# The IP address and the host name should be separated by at least one

# space.

#

# Additionally, comments (such as these) may be inserted on individual

# lines or following the machine name denoted by a '#' symbol.

#

# For example:

#

#      102.54.94.97     rhino.acme.com          # source server

#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.

# 127.0.0.1       localhost
# ::1             localhost
127.0.0.1     fx.com

Php服务器开启+开代理对接接口(综合)

后台人员本地+前端本地开发  全局域网可以访问

image.png

image.png

image.png