更改环回 lb-services.js 的基数 URL
Changin the base URL of loopback lb-services.js
我正在尝试连接一个客户端服务器和一个休息 api 服务器。我在前端使用 angular js,在后端使用环回。
在 lb-services.js 我将基础 url 更改为:
var urlBase = 'http://localhost:3000/api';
我的 angular js 在端口 4000 上是 运行。但是当我对其余 api 进行 post 时,我在浏览器上收到此错误:
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, []);
});
您可以将 CORS 支持添加到您的 LoopBack API:
https://docs.strongloop.com/display/public/LB/Security+considerations#Securityconsiderations-CORS
Is there anyway I can proxy the connection or make both servers work
together properly?
令人惊讶的是,您收到此错误是因为环回默认启用 CORS。值得检查环回服务器中的 middleware.json 文件并查看 cors.params.origin 是否为真。 Here 是文档 link 供您参考。
我不确定您是如何更改 urlBase 以访问您的其余部分 api。我已经按照 here.
所述使用 angular 模块配置完成了它
我正在尝试连接一个客户端服务器和一个休息 api 服务器。我在前端使用 angular js,在后端使用环回。
在 lb-services.js 我将基础 url 更改为:
var urlBase = 'http://localhost:3000/api';
我的 angular js 在端口 4000 上是 运行。但是当我对其余 api 进行 post 时,我在浏览器上收到此错误:
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, []);
});
您可以将 CORS 支持添加到您的 LoopBack API:
https://docs.strongloop.com/display/public/LB/Security+considerations#Securityconsiderations-CORS
Is there anyway I can proxy the connection or make both servers work together properly?
令人惊讶的是,您收到此错误是因为环回默认启用 CORS。值得检查环回服务器中的 middleware.json 文件并查看 cors.params.origin 是否为真。 Here 是文档 link 供您参考。
我不确定您是如何更改 urlBase 以访问您的其余部分 api。我已经按照 here.
所述使用 angular 模块配置完成了它