moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
295 stars 118 forks source link

Q&A: How can I remove the service name from the api endpoint? #315

Closed nnthuan closed 2 years ago

nnthuan commented 2 years ago

Q&A: How can I remove the service name from the API endpoint?

When run service, API endpoint like this

/${SERVICE NAME}/products/:id <-- Auto

Expected: /products/:id


"moleculer": "^0.14.24", "moleculer-web": "^0.10.4",

intech commented 2 years ago

@nnthuan How exactly are you configuring the REST?

nnthuan commented 2 years ago

Hi @intech

export default class ApiService extends Service {

  public constructor(broker: ServiceBroker) {
    super(broker);
    this.parseServiceSchema({
      name: "ProductService",
      mixins: [ApiGateway],
      // More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
      settings: {
        port: process.env.PORT || 3000,
        routes: [{
          path: "/",
          ...
          }]
       },

      actions: {
               getProduct: {
                   rest: "GET /products/:id",
                   ....
               }
       }

Expected: http://localhost:3000/products/:id

Actual: http://localhost:3000/ProductService/products/:id

I don't want to have /ProductService in my endpoint.

intech commented 2 years ago
export default class ApiService extends Service {

  public constructor(broker: ServiceBroker) {
    super(broker);
    this.parseServiceSchema({
      name: "ProductService",
      mixins: [ApiGateway],
      // More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
      settings: {
        port: process.env.PORT || 3000,
        routes: [{
          path: "/",
          aliases: {
              "GET /products/:id": "ProductService.getProduct"
          }
          ...
          }]
       },

      actions: {
               getProduct: {
                   ....
               }
       }
nnthuan commented 2 years ago

I see doing it manually through the alias, but API Gateway publishes APIs from various services, which is tired with a large amount.

Is there a setting that applies to all?

intech commented 2 years ago

@nnthuan Generation automatically goes according to the rootApiPath/serverSettingsRestPath/action template. By adding step by step to the root settings of the path, setting the service path (including the version and name or option specified in the REST settings), the action path from its name (or from the REST option).

nnthuan commented 2 years ago

Resolved

export default class ApiService extends Service {

  public constructor(broker: ServiceBroker) {
    super(broker);
    this.parseServiceSchema({
      name: "ProductService",
      mixins: [ApiGateway],
      // More info about settings: https://moleculer.services/docs/0.14/moleculer-web.html
      settings: {
        port: process.env.PORT || 3000,

         rest: '/',   // <========== HERE

        routes: [{
          path: "/",
          ...
          }]
       },

      actions: {
               getProduct: {
                   rest: "GET /products/:id",
                   ....
               }
       }