Apache服务器上的Angular应用程序找不到后端
我有一个Angular 6应用程序,我从Apache服务器上的Angular应用程序找不到后端,angular,apache,apache2,Angular,Apache,Apache2,我有一个Angular 6应用程序,我从npm start开始,后端配置为proxy.conf.json配置: { "/api/*": { "target": "http://123.123.1.23:8080", "secure": false, "logLevel": "debug", "changeOrigin": true } } 应用程序工作正常,但当我使用ng build(不带-prod)编译它并将其部署在Apache服务器上的/var/ww
npm start
开始,后端配置为proxy.conf.json配置:
{
"/api/*": {
"target": "http://123.123.1.23:8080",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
应用程序工作正常,但当我使用ng build
(不带-prod)编译它并将其部署在Apache服务器上的/var/www/html下时,我可以打开网页,但与后端的通信可能无法工作,因为无法发现它
我在/etc/apache2/sites enabled/000 default.conf
<Directory "/var/www/html">
AllowOverride All
</Directory>
允许超越所有
但我还是无法登录。您知道我的配置中缺少什么吗?我的一个项目上的Http拦截器示例: 其中apiUrl是在environment.ts文件上定义API url的位置
@Injectable()
export class HttpRequestInterceptorService implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const url = environment.apiUrl;
req = req.clone({
url: url + req.url,
headers: new HttpHeaders({
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
})
});
return next.handle(req)
.pipe(catchError(error => {
if ((<HttpErrorResponse>error).status === 401) {
console.log(error);
}
return throwError(error);
}));
}
}
然后在app.module.ts上注册
WebRequestInterceptorModule.forRoot()
据我所知,代理仅用于开发时间。它从来不是用于生产的。好的,我应该使用什么配置?IMO,最好是使用http拦截器。你能给我举个例子吗?你能把答案稍微扩展一下吗?我应该在哪个文件和位置添加这些配置?
@NgModule({
declarations: [],
imports: [
CommonModule,
HttpClientModule
],
providers: [
]
})
export class WebRequestInterceptorModule {
constructor(@Optional() @SkipSelf() parentModule: WebRequestInterceptorModule) {
if (parentModule) {
throw new Error('WebRequestInterceptorModule is already loaded. Import it in AppModule only');
}
}
static forRoot(): ModuleWithProviders {
return {
ngModule: WebRequestInterceptorModule,
providers: [
{provide: HTTP_INTERCEPTORS, useClass: HttpRequestInterceptorService, multi: true}
]
};
}
}