Express gateway return Cannot GET

I want to create an api gateway with express-gateway (https://www.express-gateway.io/). I have a service as "datastore"

and datastore host: mydatastore.us-west-2.compute.amazonaws.com

I want to create a gateway for : mydatastore.us-west-2.compute.amazonaws.com/datastore

So my gateway.config.yml file like that

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  datastore:
    host: mydatastore.us.us-west-2.compute.amazonaws.com
    methods : [ 'POST', 'GET' ]
    paths: ['/' ,'/datastore']
serviceEndpoints:
  datastoresrv:
    url: 'http://mydatastore.us.us-west-2.compute.amazonaws.com/'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  user:
    apiEndpoints:
      - datastore
    policies:
      - proxy:
          - action:
              serviceEndpoint: datastoresrv
              changeOrigin: true

but when I send a request to localhost:8080/ or localhost:8080/datastore I getting 404 status code and message : "Cannot GET /" or "Cannot GET /datastore/"

So how can I solve this error. Please help.

Upvotes: 0

Views: 306

Answers (2)

Railway
Railway

Reputation: 71

I am not sure why you wrote user: under the pipelines

I recommend you to change it like below:

Your code:

pipelines:
  user:
    apiEndpoints:
      - datastore
    policies:

Recommendation:

pipelines:
  - name: datastore
    apiEndpoints:
      - datastore
    policies:

And please use this datastore name in the serviceEndpoints like below Old one:

serviceEndpoints:
  datastoresrv:
    url: 'http://mydatastore.us.us-west-2.compute.amazonaws.com/'

Recommendation:

serviceEndpoints:
  datastoresrv:
    url: 'http://datastore/'

If you still have a trouble, you might try to change the policies like below

policies:
      - cors:
          - action:
              origin: '*'
              methods: 'POST,GET,PUT,PATCH,DELETE'
              allowedHeaders: 'Content-Type,Origin,Authorization,Host,Accept,X-Requested-With'
              preflightContinue: true
      - proxy:
          - action:
              serviceEndpoint: datastoresrv
              changeOrigin: true

In addition, I am attching my working config code that you might need to compare with yours.

http:
  port: 11000
admin:
  port: 9876
apiEndpoints:
  api:
    paths: /ip
  hr:
    paths: /api/auth/*
  hrTokenSignin:
    paths: /api/token/*
serviceEndpoints:
  httpbin:
    url: https://httpbin.org
  hrService:
    url: "http://hr:8000"
policies:
  - jwt
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  - name: hr
    apiEndpoints:
      - hr
    policies:
      - cors:
          - action:
              origin: '*'
              methods: 'POST,GET,PUT,PATCH,DELETE'
              allowedHeaders: 'Content-Type,Origin,Authorization,Host,Accept,X-Requested-With'
              preflightContinue: true
      - proxy:
          - action:
              serviceEndpoint: hrService
              changeOrigin: true
  - name: hrTokenSignin
    apiEndpoints:
      - hrTokenSignin
    policies:
      - cors:
        - action:
            origin: '*'
      - jwt:
        - action:
            secretOrPublicKey: "crm-secret-key"
            checkCredentialExistence: "false"
      - proxy:
          - action:
              serviceEndpoint: hrService
              changeOrigin: true
  - name: api
    apiEndpoints:
      - api
    policies:
      - cors:
          - action:
              origin: '*'
              methods: 'POST,GET,PUT,PATCH,DELETE'
              allowedHeaders: 'Content-Type,Origin,Authorization,Host,Accept,X-Requested-With'
              preflightContinue: true
      - proxy:
          - action:
              serviceEndpoint: httpbin

Upvotes: 0

James McLeod
James McLeod

Reputation: 2406

The problem is in the definition of your datastore apiEndpoint; the hostname has to match the API gateway's host name, in your case, localhost:

apiEndpoints:
  datastore:
    host: localhost
    methods : [ 'POST', 'GET' ]
    paths: ['/' ,'/datastore']

Upvotes: 0

Related Questions