proxy.config.json: more
{
"/api": {
"target": "https://api.domain.com",
"secure": false,
"changeOrigin": true
}
}
angular.json: proxyConfig
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"development": {
...
"proxyConfig": "proxy.config.json"
}
},
...
},
...
"serve-ssr": {
"builder": "@nguniversal/builders:ssr-dev-server",
"configurations": {
"development": {
...
"proxyConfig": "proxy.config.json"
},
...
},
app.server.module.ts: useAbsoluteUrl
import {
INITIAL_CONFIG,
PlatformConfig,
ServerModule,
} from '@angular/platform-server';
...
const platformConfig: PlatformConfig = { useAbsoluteUrl: true };
...
@NgModule({
imports: [AppModule, ServerModule],
bootstrap: [AppComponent],
providers: [
...
{ provide: INITIAL_CONFIG, useValue: platformConfig },
],
server.ts: api proxy
import { createProxyMiddleware, Options } from 'http-proxy-middleware';
...
const proxyFile = join(process.cwd(), 'proxy.config.json');
if (existsSync(proxyFile)) {
const json = readFileSync(proxyFile).toString();
const config: Record<string, Options> = JSON.parse(json);
Object.keys(config).forEach((key) => {
server.use(key, createProxyMiddleware(config[key]));
});
}