如果我代理本地主机服务器,那么带有http-proxy-middleware的浏览器是否可以脱机工作吗?
我在localhost:3000
部署了一个角度应用程序,要求在localhost:8080
部署api服务器。对api-server的http请求由Browsersync http-proxy-middleware
代理。如果我有互联网连接,一切正常,但如果我离线,我在gulp控制台上收到以下错误:
[HPM] Proxy error: ENOENT localhost:8080/data/pet
在浏览器控制台上:
Failed to load resource: the server responded with a status of `500 (Internal Server Error)`
我知道服务器上没有错误,因为我仍然可以通过浏览器访问localhost:8080/data/pet
。
由于api-server是在本地部署的,因此我需要一个互联网连接代理http请求似乎很奇怪。
角度应用程序及其所有配置都是使用generator-gulp-angular
生成的这是我的gulp服务器配置:
'use strict';
var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');
var browserSync = require('browser-sync');
var browserSyncSpa = require('browser-sync-spa');
var util = require('util');
var proxyMiddleware = require('http-proxy-middleware');
function browserSyncInit(baseDir, browser) {
browser = browser === undefined ? 'default' : browser;
var routes = null;
if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) {
routes = {
'/bower_components': 'bower_components'
};
}
var server = {
baseDir: baseDir,
routes: routes
};
server.middleware = proxyMiddleware('/data',
{
target: 'http://localhost:8080'
});
browserSync.instance = browserSync.init({
startPath: '/',
server: server,
browser: browser,
online: false
});
}
browserSync.use(browserSyncSpa({
selector: '[ng-app]'// Only needed for angular apps
}));
gulp.task('serve', ['watch'], function () {
browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]);
});
gulp.task('serve:dist', ['build'], function () {
browserSyncInit(conf.paths.dist);
});
gulp.task('serve:e2e', ['inject'], function () {
browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []);
});
gulp.task('serve:e2e-dist', ['build'], function () {
browserSyncInit(conf.paths.dist, []);
});
这是我的package.json:
{
"name": "petStore",
"version": "0.0.0",
"dependencies": {},
"scripts": {
"test": "gulp test"
},
"devDependencies": {
"gulp": "~3.9.0",
"gulp-autoprefixer": "~2.3.1",
"gulp-angular-templatecache": "~1.6.0",
"del": "~1.2.0",
"lodash": "~3.9.3",
"gulp-csso": "~1.0.0",
"gulp-filter": "~2.0.2",
"gulp-flatten": "~0.0.4",
"gulp-jshint": "~1.11.0",
"gulp-load-plugins": "~0.10.0",
"gulp-size": "~1.2.1",
"gulp-uglify": "~1.2.0",
"gulp-useref": "~1.2.0",
"gulp-util": "~3.0.5",
"gulp-ng-annotate": "~1.0.0",
"gulp-replace": "~0.5.3",
"gulp-rename": "~1.2.2",
"gulp-rev": "~5.0.0",
"gulp-rev-replace": "~0.4.2",
"gulp-minify-html": "~1.0.3",
"gulp-inject": "~1.3.1",
"gulp-protractor": "~1.0.0",
"gulp-sourcemaps": "~1.5.2",
"gulp-less": "~3.0.3",
"gulp-angular-filesort": "~1.1.1",
"main-bower-files": "~2.8.0",
"merge-stream": "~0.1.7",
"jshint-stylish": "~2.0.0",
"wiredep": "~2.2.2",
"karma": "~0.12.36",
"karma-jasmine": "~0.3.5",
"karma-phantomjs-launcher": "~0.2.0",
"karma-angular-filesort": "~0.1.0",
"karma-ng-html2js-preprocessor": "~0.1.2",
"concat-stream": "~1.5.0",
"require-dir": "~0.3.0",
"browser-sync": "~2.7.12",
"browser-sync-spa": "~1.0.2",
"http-proxy-middleware": "~0.2.0",
"chalk": "~1.0.0",
"uglify-save-license": "~0.4.1",
"wrench": "~1.5.8"
},
"engines": {
"node": ">=0.10.0"
}
}
这是http电话:
$http.get("/data/pet").then(function (result) {
vm.petName = result.data.name;
});
和bower.json
{
"name": "petStore",
"version": "0.0.0",
"dependencies": {
"angular-resource": "~1.4.0",
"angular-ui-router": "~0.2.15",
"bootstrap": "~3.3.4",
"angular-bootstrap": "~0.13.0",
"malarkey": "yuanqing/malarkey#~1.3.0",
"toastr": "~2.1.1",
"moment": "~2.10.3",
"animate.css": "~3.3.0",
"angular": "~1.4.0"
},
"devDependencies": {
"angular-mocks": "~1.4.0"
},
"resolutions": {
"angular": "~1.4.0"
}
}
答案 0 :(得分:3)
刚做了一些测试,我可以确认这个问题。 用以下方法进行库测试:" npm test"。 它将设置实际的服务器来运行一些测试。
在线时,所有测试都会成功。 但是当离线运行测试时,它将产生相同的错误消息。
实际的代理功能由node-http-proxy提供。
此问题已在以下网站报道: https://github.com/nodejitsu/node-http-proxy/issues/835
我也很好奇为什么http-proxy不能脱机使用。
(免责声明:我是http-proxy-middleware的作者。)
答案 1 :(得分:0)
看起来核心browsersync不需要互联网连接。有些功能可以。
以下设置可让您将browsersync切换为离线模式:https://browsersync.io/docs/options/#option-online
我在server.js文件中设置了此属性,我的应用程序脱机工作:
browserSync.instance = browserSync.init({
startPath: '/',
server: server,
browser: browser,
online: false
});