简体   繁体   English

使用回送REST API时如何更改gulp-angular项目的端口号

[英]How to change the port number of the gulp-angular project when using loopback REST API

I´m making a project using loopback on the backend and angular on my frontend. 我正在使用后端的环回和前端的angular进行项目。 The client side was generated with gulp-angular. 客户端是用gulp-angular生成的。 After that I added the lb-services.js from loopback. 之后,我从环回添加了lb-services.js。 The problem I´m having is that the angular app is trying to make requests to the API on the same port I have the gulp serve running . 我遇到的问题是,Angular应用程序试图在gulp serve running的同一端口上向API发出请求。 I have my loopback app on port 3000 and Angular runs on port 3001. 我在端口3000上有我的回送应用程序,而Angular在端口3001上运行。

How can I tell Angular to change the port number when making request to the API? 向API发出请求时,如何告诉Angular更改端口号?

I´ve tried modifying the gulp server.js like this: 我试图像这样修改gulp server.js

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

But this just makes both angular and the requests it makes on the API run on port 4000, getting me this result: 但这只会使角度和在API上发出的请求都在端口4000上运行,从而得到以下结果:

Angular running on: http://localhost:4000/ Angular运行在: http://localhost:4000/

Request to loopback server: 请求回送服务器:

http://localhost:4000/api/People 

What I want is this: Angular running on: http://localhost:4000/ 我想要的是:Angular运行在: http://localhost:4000/

Request to loopback server: 请求回送服务器:

http://localhost:3000/api/People 

Can anyone help me achive this? 谁能帮我实现这一目标?

You can add a rewriteRule for the API to change the URL to localhost:3000. 您可以为API添加rewriteRule,以将URL更改为localhost:3000。 As shown here: https://www.browsersync.io/docs/options/#option-rewriteRules 如此处所示: https : //www.browsersync.io/docs/options/#option-rewriteRules

Another option that's available for you is this: https://github.com/Swiip/generator-gulp-angular/blob/master/docs/how-it-works.md#proxy . 可供您使用的另一个选项是: https : //github.com/Swiip/generator-gulp-angular/blob/master/docs/how-it-works.md#proxy You can use a proxy to redirect the API request to the correct URL. 您可以使用代理将API请求重定向到正确的URL。 Like this (taken from Github example): 像这样(摘自Github示例):

app.use('/api', proxy({target: 'http://localhost:3000/api', changeOrigin: true}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM