API获取应该是前端还是后端?

编程入门 行业动态 更新时间:2024-10-23 11:27:33
本文介绍了API获取应该是前端还是后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的网站应用程序将React用于前端,而我决定在ExpressJS或Flask.py之间作为后端.无论哪种方式,我都知道React具有API提取功能,但也有其他后端专用框架.我的问题是,按惯例,API提取逻辑是前端还是后端的一部分?

My website application is using React for the frontend, and I'm deciding between ExpressJS or Flask.py for the backend. Either way, I know React has API fetching functions but also the other backend-dedicated frameworks. My question is, conventionally, would API fetching logic be part of the frontend or backend?

推荐答案

在某些情况下,要求或更喜欢在后端进行API提取:

There are some conditions which require or prefer API fetching to be in the back-end:

  • 当您需要使用安全凭据来访问API时,这些凭据需要保密.

  • When you need to use security credentials to access the API and you those credentials need to stay private.

    当API不允许跨源访问,因此您的前端无法直接访问它.

    When the API does not permit cross origin access so your front-end could not directly access it.

    当您需要对API结果进行一些处理并且希望将处理算法保留为商业秘密时(请注意,在前端完成的所有处理均可用供任何编码人员查看).前端代码中没有秘密.

    When you need to do some processing on the API results and you wish to keep the processing algorithms a trade secret (keep in mind that all processing done in the front-end is available for any coder to see). There are no secrets in the front-end code.

    当前端设备可能没有适当的资源(CPU,内存等)来处理API结果时.小型专用设备尤其如此.

    When the front-end device may not have appropriate resources (CPU, memory, etc..) to process the API results. This can particularly be the case with smaller dedicated devices.

    当您不希望前端设备必须消耗电池电量来定期访问API时.如果您定期轮询某些API以查找更改,则可能尤其如此.在这种情况下,您可能希望后端进行轮询,而只是让前端知道何时实际需要执行一些操作.

    When you don't want the front-end device to have to expend the battery energy to regularly access the API. This might particularly be the case if you're regularly polling some API looking for changes. In that case, you might want the back-end to do the polling and just let the front-end know when there is something to actually do.

    如果出于某些原因需要对访问API进行一些集中式性能管理,例如结果缓存或请求限制.

    When there are reasons to do some centralized performance management of your access to the API such as caching of results or request throttling.

    当所有前端设备可能没有对API服务器的正确网络访问权限.

    When all front-end devices may not have proper network access to the API server.

    如果将API访问置于后端没有明显的好处,那么(对于您的服务器)扩展性可能更大,以使前端能够尽其所能.

    If there are no compelling benefits to putting the API access in the back-end, then it may be more scalable (for your server) to let the front-end do as much work as it can.

  • 更多推荐

    API获取应该是前端还是后端?

    本文发布于:2023-11-15 21:23:11,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1598067.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应该是   后端   API

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!