Files
hexo-blog/source/_posts/browser-sync代理接口在ie8下返回406问题.md
T
2020-11-24 17:45:32 +08:00

62 lines
2.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: browser-sync代理接口在IE8下返回406问题
id: '64'
categories:
- 分享
tags:
- browser-sync
- 代理
- IE8
- 406
date: 2016-06-28 10:15:34
---
正常情况下,代理返回的头信息为: HTTP/1.1 200 OK server?? ? Apache-Coyote/1.1 content-type?? ?application/json; charset=UTF-8 transfer-encoding?? ?chunked date?? ? Tue, 28 Jun 2016 02:07:39 GMT connection?? ?close   然而在IE8下面却返回了下面的头信息: HTTP/1.1 406 Not Acceptable server: Apache-Coyote/1.1 set-cookie: JSESSIONID=7F367B9EB53FDC8306D807E0A7BD4201; Path=/; HttpOnly content-type: text/html; charset=utf-8 date: Tue, 28 Jun 2016 02:01:31 GMT connection: close Transfer-Encoding: chunked   通过对比头信息,在代理设置里做了一下判断,在低版本IE浏览器重新设置一下头信息。
``` js
var browserSync = require('browser-sync');
var proxyMiddleware = require('http-proxy-middleware');
var proxy_context = ['/service/web'];
var proxy_option = {
target: 'http://' + proxyConfig.host + ':' + proxyConfig.port,
//增加一个错误处理在window 7 ie7,8
onProxyReq: function(proxyReq, req, res) {
var is4dot0 = false;
var acceptValue = "";
var acceptEncodeing = "";
var rawHeaders = req.rawHeaders;
rawHeaders.forEach(function(value, index) {
if (value === "Accept") {
acceptValue = rawHeaders[index + 1];
}
if (value === "Accept-Encoding") {
acceptEncodeing = rawHeaders[index + 1];
}
if (value.indexOf("Mozilla/4.0") === 0) {
is4dot0 = true;
}
});
if (is4dot0) {
proxyReq.setHeader("Accept", acceptValue);
proxyReq.setHeader("Accept-Encoding", acceptEncodeing);
}
}
};
var proxy = proxyMiddleware(proxy_context, proxy_option);
browserSync({
host: 'local.xxx.com',
port: 3000,
open: 'external',
browser: 'google chrome',
ghostMode: false, // 每个页面的镜像独立,不相互影响
server: {
baseDir: "build",
middleware: [proxy]
}
});
```