博客
关于我
vue项目配置文件vue.config.js中devServer.proxy 使用说明
阅读量:353 次
发布时间:2019-03-04

本文共 1137 字,大约阅读时间需要 3 分钟。

devServer.proxy 说明

在前后端分离的项目中,通常前端应用和后端API 服务器没有运行在同一个主机上;在开发环境下将API 请求代理到API 服务器可以通过 vue.config.js 中的 devServer.proxy 选项来配置;

module.exports = {     devServer: {     	host:'0.0.0.0,  	port: 2000,  	open: true,    proxy: {         '/api': 'http://localhost:3000',    },  }}

现在,对 http://localhost:2000/api/users 的请求会将请求代理到 http://localhost:3000/api/users;

如果不需要传递 /api ,则需要重写路径:

module.exports = {     devServer: {     	host:'0.0.0.0,  	port: 2000,  	open: true,    proxy: {         '/api': {         		target: `http://localhost:3000`,      		pathRewrite: {    '^/api': '' },      }    },    disableHostCheck: true  }}

现在对 http://localhost:2000/api/users 的请求将会代理到 http://localhost:3000/users ;

默认情况下,代理时会保留主机头的来源,可以将changeOrigin 设置为true 以此覆盖此行为;

module.exports = {     devServer: {     	host:'0.0.0.0,  	port: 2000,  	open: true,    proxy: {         '/api': {         		target: `http://localhost:3000`,      		pathRewrite: {    '^/api': '' },      		changeOrigin: true,      }    },    disableHostCheck: true  }}

注意:

在代理请求时,浏览器控制台 NetWork 中显示的请求路径依然是未代理前的路径,即 请求依然 显示为 http://localhost:2000/api/users ;只要保证 请求路径 以/api 开头,这原请求路径将被代理到 目标 路径。

转载地址:http://yryr.baihongyu.com/

你可能感兴趣的文章
Nginx运维与实战(一)-Nginx不同场景使用方法
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx部署_mysql代理_redis代理_phoenix代理_xxljob代理_websocket代理_Nacos代理_内网穿透代理_多系统转发---记录021_大数据工作笔记0181
查看>>
nginx部署本地项目如何让异地公网访问?服务器端口映射配置!
查看>>
Nginx配置HTTPS服务
查看>>
Nginx配置https的一个误区(导致404错误)
查看>>
Nginx配置Https证书
查看>>
Nginx配置http跳转https
查看>>
Nginx配置ssl实现https
查看>>
nginx配置ssl证书https解决公网ip可以访问但是域名不行的问题
查看>>
Nginx配置TCP代理指南
查看>>
NGINX配置TCP连接双向SSL
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
查看>>
Nginx配置中root和alias分不清?本文3分钟帮你解惑!
查看>>
nginx配置中的服务器名称
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
nginx配置全解
查看>>
Nginx配置参数中文说明
查看>>
Nginx配置后台网关映射路径
查看>>