简体   繁体   English

如何通过 RESTAPI 在 Express Gateway 中使用多个路径和端点?

[英]How do i use multiple paths and endpoints in Express Gateway with my RESTAPI?

Currently working on an Express Gateway that handles call for an RESTAPI and a GraphQL microservices.目前正在开发处理对 RESTAPI 和 GraphQL 微服务的调用的 Express Gateway。 The GraphQL pipeline works fine, but the pipeline for the RESTAPI is what I'm struggling with. GraphQL 管道工作正常,但 RESTAPI 的管道是我正在努力解决的问题。

I made a simple CRUD functionality RESTAPI that can create, read, update and delelete books and authors.我制作了一个简单的 CRUD 功能 RESTAPI,可以创建、阅读、更新和删除书籍和作者。 They have multiple routes to do this, like: http://localhost:4001/books/add.他们有多种途径可以做到这一点,例如: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.问题是我真的不明白如何在快速网关中翻译这些路由或路径,以便我可以通过网关到达它们。

This is my current code, config.yml:这是我当前的代码,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

This is the restapi book code:这是restapi书的代码:

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;

Now when I call: http://localhost:4001/rp, it return "restapi" just like i told to do it so.现在,当我调用:http://localhost:4001/rp 时,它会返回“restapi”,就像我告诉的那样。 But when I call: http://localhost:4001/rp/books, it returns a "CANNOT GET", which is logical cause I didnt define this path.但是当我调用:http://localhost:4001/rp/books 时,它返回一个“CANNOT GET”,这是合乎逻辑的,因为我没有定义这个路径。 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.我希望有人能给我解释一下,因为快递网关没有像我这样的例子。 :) :)

I found the solution.我找到了解决方案。

I was confused with the definitions between an apiEndpoint and a serviceEndpoint.我对 apiEndpoint 和 serviceEndpoint 之间的定义感到困惑。

The answer is: yes you have to hardcode all the paths in, but this will only be under "apiEndpoints".答案是:是的,您必须对所有路径进行硬编码,但这只会在“apiEndpoints”下。

It will look like this:它看起来像这样:

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

So in summary, only one service with multiple api endpoints, which sound pretty logic.所以总而言之,只有一项服务具有多个 api 端点,这听起来很合逻辑。

I think a con is that when there are a lot of services this config file will become a big mess of endpoints.我认为一个缺点是,当有很多服务时,这个配置文件将变成一大堆端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node Express Gateway多个API端点 - Node Express Gateway multiple API endpoints 如何设置快速网关以安全地连接我的服务 - How do I set up express gateway to securely connect with my services 如何在我的 restAPI 中进行更改密码路由? - How do I go about making the change password route in my restAPI? 如何仅使用 Apollo Server 2 graphql 端点的快速中间件 - How can I use express middleware with only Apollo Server 2 graphql endpoints 为什么我的异步 express.js 中间件出现 502 Bad Gateway 错误? - Why do i get 502 Bad Gateway error in my async express.js middleware? 如何使用快速网关将多个响应与单个网关端点相结合 - How to combined multiple responses with single gateway endpoint using express gateway 当多个路径匹配时,Express如何知道要使用哪个路由器路径? - How does Express know which Router path to use when multiple paths match? 如何同时使用Express和Ampersand? - How do I use Express and Ampersand simultaneously? 你如何在快递应用程序中设置基于jade的选项? (“extendsir”选项需要使用“extends”和“绝对”路径) - How do you set jade basedir option in an express app? (the “basedir” option is required to use “extends” with “absolute” paths) 如何在我的数据库中填充多个路径? - How can i populate multiple paths in my DB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM