Getting Started to Abandon the Node Series Express-Gateway Gateway

Foreword

The first public account of this article [a typist]

There are really few articles on this topic on the online wall. A little bit of learning, I hope to help everyone. express-gateway official website

Installation

  • Install Express Gateway
npm install -g express-gateway
  • Create a project
eg gateway create
  • Initial configuration of some basic information
eg gateway create< br />? What is the name of your Express Gateway? my-gateway
? Where would you like to install your Express Gateway? my-gateway
? What type of Express Gateway do you want to create? (Use arrow keys)
❯ Getting Started with Express Gateway
Basic (default pipeline with proxy)
  • Start
cd my -gateway && npm start

Such a basic project has taken shape.

How to use

Specify the service and expose the API (API provider)

  • First step

< p>We will find that an existing service https://httpbin.org/ip has been proxied in the project and managed. It provides a json output, we can show the function of the fast gateway. Visit https://httpbin.org/ip, there will be the following output:

{
"origin": "218.80.1.67" #自己的IP
}
  • The second step

The service is designated as the default in the express gateway On the server side of the pipeline, the gateway has its own proxy strategy. The previous https://httpbin.org/ip is a service request set by the gateway. The configuration is in the gateway.config.yml file in the config directory.

http:
port: 8080
admin:
port: 9876
hostname: localhost
apiEndpoints:
api:
host: localhost
paths:'/ip'
serviceEndpoints:
httpbin:
url:'https://httpbin.org'
policies:
-basic-auth
-cors
-expression
-key-auth
-log
-oauth2
-proxy
-rate -limit
pipelines:
default:
apiEndpoints:
-api
policies:
# Uncomment `key-auth:` when instructed to in the Getting Started guide.
#-key-auth:
-proxy:
-action:
serviceEndpoint: httpbin
changeOrigin: true

You can see A default service endpoint is configured here, and the gateway will find httmbin in the default proxy policy.

  • The third step

Next we need to make httpbin The service passes through the gateway as an API endpoint, and when the API is exposed, it can be accessed from the outside.
In the above gateway.config.yml configuration file, we find the apiEndpoints setting, and there is a setting item for api.

apiEndpoints:
api:
host: localhost
paths:'/ip'

PS: By default, the API request path will be The proxy policy is hung in the service endpoint.

  • Step Four

Now we can access the API through the gateway, visit http://localhost:8080/ip.

Create API consumers

To facilitate API management, we call authorized users who are allowed to use APIs as “consumers.” For the creation of users, enter the project root directory and create:

eg users create
Enter username [required]: mrpan
Enter firstname [required]: coder
Enter lastname [required]: coder
Enter email: [email protected]
Enter redirectUri: www.baidu.com
√ Created 892775c8-80c5-480e-b596-6cb3133691f2

"firstname": "coder",
"lastname": "coder",
"email": "[email protected]",
"redirectUri": "www.baidu .com",
"isActive": true,
"username": "mrpan",
"id": "892775c8-80c5-480e-b596-6cb3133691f2",
"createdAt": "Sat Dec 16 2017 13:21:13 GMT+0800 (China Standard Time)",
"updatedAt": "Sat Dec 16 2017 13:21:13 GMT+0800 (China Standard Time) "

API authorization authentication

  • The first step

Now our API is public, and there is no authorization control, so Anyone can access it. We now use key authorization to protect it. First, we must add this authorization policy to the configuration file gateway.config.yml.

pipelines:
-name: getting-started
apiEndpoints:
-api
policies:
-key-auth:
-proxy:
-action:
serviceEndpoint: httpbin
changeOrigin: true
  • The second step

Distribute the key to the user above mrpan

eg credentials create -c mrpan -t key-auth -q
10b9Yaalb982DreBukZvGf:3A2bhd1xzqwAvNWX0QfjD5

PS: The above -q option is to limit the output to the api key, which is convenient for pasting and copying.

  • The third step

Visit again http://localhost:8080 /ip, the result will not be printed at this time, only Unauthorized will be printed.

  • Step Four

We add the key when we visit. In this way, you can continue to use the API.

At this point, the basic use of the gateway is almost the same. You can sort out and expand it in your own system.

Conclusion

Currently, most large-scale website architectures have adopted the microservice model, splitting the system into microservices one by one, and the service layer may be written in java or in other languages After all, there is a precedent like Netflix, successfully using Node.js API gateway and its JAVA backend to support a wide range of clients.

Leave a Comment

Your email address will not be published.