如何通过我的RESTAPI在Express Gateway中使用多个路径和端点? [英] How do i use multiple paths and endpoints in Express Gateway with my RESTAPI?

查看:43
本文介绍了如何通过我的RESTAPI在Express Gateway中使用多个路径和端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在处理Express网关,该网关处理对RESTAPI和GraphQL微服务的调用.GraphQL管道工作正常,但是我正在努力使用的RESTAPI管道.

Currently working on an Express Gateway that handles call for an RESTAPI and a GraphQL microservices. The GraphQL pipeline works fine, but the pipeline for the RESTAPI is what I'm struggling with.

我制作了一个简单的CRUD功能RESTAPI,可以创建,阅读,更新和删除书籍和作者.他们可以通过多种途径来执行此操作,例如:http://localhost:4001/books/add.

I made a simple CRUD functionality RESTAPI that can create, read, update and delelete books and authors. They have multiple routes to do this, like: http://localhost:4001/books/add.

问题是我不太了解如何在快速网关中转换这些路线或路径,因此我无法通过网关到达它们.

The problem is that I dont really understand how to translate these routes or paths in tho the express gateway so I can reach them via the gateway.

这是我当前的代码config.yml:

This is my current code, config.yml:

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  restapi:
    host: localhost
    paths: '/rp'
  graphql:
    host: localhost
    paths: '/gql'
serviceEndpoints:
  restapi:
    url: 'http://localhost:4001/'    
  graphql:  
    url: 'http://localhost:4000'
policies:
  - proxy
pipelines:
  restapi:
    apiEndpoints:
      - restapi
    policies:
      - proxy:
          - action: 
              serviceEndpoint: restapi
              changeOrigin: true
              ignorePath: false
              prependPath: true
              stripPath: true
              
  graphql:
    apiEndpoints:
      - graphql
    policies:
      - proxy:
          - action:
              serviceEndpoint: graphql
              changeOrigin: true

这是restapi的书代码:

This is the restapi book code:

const express = require('express');
const mongoose = require('mongoose');
const book = require('../models/book');
const { findById } = require('../models/book');
const router = express.Router();
const Book = require('../models/book');

//read all books
router.get('/', async (req, res) =>{
    try{
        const AllBooks = await Book.find();
        res.json(AllBooks);
    }catch(err){
        res.json({message:err});
    }
    
})

//create book
router.post('/add', async (req, res) => {
    var NewBook = new Book({
        title: req.body.title,
        pages: req.body.pages
    })
    try{
        const SavedBook = await NewBook.save();
        res.json(SavedBook);
    }catch(err){
        res.json({message: err})
    }
})

//read book
router.get('/:BookId', async (req, res) => {
    try{
        const ReadBook = await Book.findById(req.params.BookId);
        res.json(ReadBook);
    }catch(err){
        res.json({message: err});
    }    
})

//update book
router.patch('/update/:BookId', async (req, res) => {
    try{
        const updatedBook = await Book.updateOne({_id: req.params.BookId},
            {$set: {title: req.body.title, pages: req.body.pages}});
        res.json(updatedBook);
    }catch(err){
        res.json({message: err});
    }    
})

//delete book
router.delete('/delete/:BookId', async (req, res) => {
    try{
         const DelBook = await Book.findById(req.params.BookId);
         DelBook.delete();
         res.send(DelBook + " Deleted");
    }catch(err){
        res.json({message: err});
    }
})



module.exports = router;

现在,当我致电:http://localhost:4001/rp时,它将返回"restapi".就像我告诉我那样做.但是当我调用:http://localhost:4001/rp/books时,它返回"CANNOT GET",这是逻辑原因,因为我没有定义此路径.首先,我认为快递网关会自动理解这一点.

Now when I call: http://localhost:4001/rp, it return "restapi" just like i told to do it so. But when I call: http://localhost:4001/rp/books, it returns a "CANNOT GET", which is logical cause I didnt define this path. First I thought the express gateway would understand this automaticly.

我是否必须对所有路径进行硬编码?

Do i have to hardcode all the paths?

我希望有人可以向我解释这一点,因为快速网关没有像我这样的例子.:)

I hope somebody can explain this to me, since express gateway does not have an example like my case. :)

推荐答案

我找到了解决方案.

我对apiEndpoint和serviceEndpoint之间的定义感到困惑.

I was confused with the definitions between an apiEndpoint and a serviceEndpoint.

答案是:是的,您必须对其中的所有路径进行硬编码,但这仅在"apiEndpoints"下.

The answer is: yes you have to hardcode all the paths in, but this will only be under "apiEndpoints".

它看起来像这样:

apiEndpoints:
  restapi:
    host: localhost
    paths: 
       - '/books'
       - '/books/add'
       - '/authors'
       - '/authors/...'
serviceEndpoints:
  restapi:
    url: 'http://localhost:4001'

因此,总而言之,只有一个具有多个api端点的服务,这听起来很合逻辑.

So in summary, only one service with multiple api endpoints, which sound pretty logic.

我认为缺点是,当有很多服务时,此配置文件将成为一大堆端点.

I think a con is that when there are a lot of services this config file will become a big mess of endpoints.

这篇关于如何通过我的RESTAPI在Express Gateway中使用多个路径和端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆