Nx: How to handle API urls.

Created on 24 Mar 2018  路  1Comment  路  Source: nrwl/nx

How to handle API path for Web and Ionic. I have my all api requests in libs. And Web part doesn't need IP address where as Ionic App needs IP address. How do I differ in between them?

Most helpful comment

You could have an APIModule that takes an argument for the address, and initialize it in both your Web app and Ionic app.

libs/api/src/api.module.ts

export const API_HOST = new InjectionToken('API_HOST');

@NgModule()
export class ApiModule {
  static forRoot(host: string) {
    return {
      ngModule: ApiModule,
      providers: [{
        provide: API_HOST,
        useValue: host
      }]
    }
  }
}

apps/mobile/src/app.module.ts

imports: [
  ApiModule.forRoot(SOME_IP_ADDRESS)
]

apps/web/src/app.module.ts

imports: [
  ApiModule.forRoot('')
]

>All comments

You could have an APIModule that takes an argument for the address, and initialize it in both your Web app and Ionic app.

libs/api/src/api.module.ts

export const API_HOST = new InjectionToken('API_HOST');

@NgModule()
export class ApiModule {
  static forRoot(host: string) {
    return {
      ngModule: ApiModule,
      providers: [{
        provide: API_HOST,
        useValue: host
      }]
    }
  }
}

apps/mobile/src/app.module.ts

imports: [
  ApiModule.forRoot(SOME_IP_ADDRESS)
]

apps/web/src/app.module.ts

imports: [
  ApiModule.forRoot('')
]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zpydee picture zpydee  路  3Comments

vimalraj-a picture vimalraj-a  路  3Comments

kmkatsma picture kmkatsma  路  3Comments

markphip picture markphip  路  3Comments

joelmuskwe picture joelmuskwe  路  3Comments