오늘만살자

Express gateway 본문

마이크로서비스

Express gateway

오늘만살자 2020. 3. 26. 11:53

https://medium.com/@oyedejipeace/building-a-simple-microservices-app-with-express-gateway-a8fd49d81aeb

 

Building a simple Microservices App with Express Gateway

One of the most interesting programming problems I have worked on was building a microservice app. What made it interesting was my…

medium.com

 

 

npm install -g express-gateway

eg gateway create

cd microservices

npm install express --save

npm install @babel/core @babel/node @babel/preset-env --save-dev

================================================================================
vi .babelrc
{
    "presets": ["@babel/preset-env"]
}
================================================================================

vi user.js
import express from  'express';

let app = express();

app.get('/users', (req, res, next) => {
    res.send(["Tony","Lisa","Michael","Ginger","Food"])
})

app.listen(3000, () => {
    console.log('Server running on 3000');
})
================================================================================

vi music.js
import express from 'express';

let app = express();

app.get('/musics', (req,res,next) => {
    res.status(200).send(['Jesus', 'I love to praise your name', 'My Story', 'Days of Elijah'])
})

app.listen(8000, () => {
    console.log('Server running on 8000')
})

================================================================================
vi ./config/gateway.config.yml
http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  user:
    host: localhost
    paths: ['/users','/users/*']
  music:
    host: localhost
    paths: ['/musics','/musics/*']
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  userService:
    url: 'http://localhost:3000'
  musicService:
    url: 'http://localhost:8000'
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
  userPipeline:
    apiEndpoints:
      - user
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: userService
              changeOrigin: true
  musicPipeline:
    apiEndpoints:
      - music
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: musicService
              changeOrigin: true

================================================================================

vi server.js
import path from 'path';
import gateway from 'express-gateway';
import './user';
import './music';

gateway()
  .load(path.join(__dirname, 'config'))
  .run();
================================================================================

npm install nodemon — save-dev

================================================================================

vi package.json 
"start":"nodemon --exec bable-node server.js"    

 
Comments