gpt4 book ai didi

javascript - 更改环回 lb-services.js 的基本 URL

转载 作者:行者123 更新时间:2023-12-03 06:56:57 26 4
gpt4 key购买 nike

我正在尝试连接客户端服务器和其余 api 服务器。我在前端使用 Angular JS,在后端使用环回。

在 lb-services.js 上,我将基本 url 更改为:

  var urlBase = 'http://localhost:3000/api';

我的 Angular js 正在端口 4000 上运行。但是当我向其余 api 发帖时,我的浏览器上收到此错误:

XMLHttpRequest cannot load http://localhost:3000/api/People. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access. The response had HTTP status code 404.

我是否可以代理连接或使两台服务器正常协同工作?

这是我的 gulp/server.js:

'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
};

/*
* You can add a proxy to your backend by uncommenting the line below.
* You just have to configure a context which will we redirected and the target url.
* Example: $http.get('/users') requests will be automatically proxified.
*
* For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.9.0/README.md
*/
server.middleware = proxyMiddleware('/api', {
target: 'http://localhost:3000/api',
changeOrigin: true

});

browserSync.instance = browserSync.init({
startPath: '/',
server: server,
browser: browser,
port:4000
});
}

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, []);
});

最佳答案

Is there anyway I can proxy the connection or make both servers work together properly?

令人惊讶的是,您会收到此错误,因为环回默认启用 CORS。值得检查环回服务器中的 middleware.json 文件并查看 cors.params.origin 是否为 true。 Here是供您引用的文档链接。

我不确定您如何更改用于访问其余 api 的 urlBase。我已经使用 Angular 模块配置完成了它,如here所述。 .

关于javascript - 更改环回 lb-services.js 的基本 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com