只能从服务器端访问SvelteKit中的API。

huangapple go评论68阅读模式
英文:

Access API only from server side in SvelteKit

问题

I already have the backend with an API for my application and now wanted to add the frontend with SvelteKit. Since I dont want my API to be available on the public internet I thought it would be a good idea to deploy the server in the same network as the API and only access it from server side. Now I'm stuck on how to exactly do that.

我已经有了应用程序的后端和API,现在想要使用SvelteKit添加前端部分。由于我不希望我的API在公共互联网上可用,我认为将服务器部署在与API相同的网络中并仅从服务器端访问它是个好主意。现在我不知道如何确切地做到这一点。

I am already using the server load function which works perfectly fine when the page is first loaded, but isnt suitable to load more data on a button press (as far as I understand).

我已经在使用服务器加载函数,当页面首次加载时它运行得非常好,但似乎不适合在按下按钮时加载更多数据(就我所了解的而言)。

Another possibility which I am aware of are form actions to post data, but I haven't been able to make them work with a 3rd party library.

我知道的另一种可能性是使用表单操作来提交数据,但我无法使它们与第三方库一起工作。

A more universal approach seems to be the handle server hook. Is it suitable for my usecase? And how does an example request triggering the hook look like? In the docs only the example code for the server.js is given and it's not clear to me which requests will be caught there.

一个更通用的方法似乎是使用handle server hook。它适用于我的用例吗?触发该钩子的示例请求是什么样的?文档中只提供了server.js的示例代码,我不太清楚哪些请求会在那里捕获。

If this is actually a good idea might be a question for another post but if there are better / common approaches to achieve exactly what I am trying to do, feel free to leave some information. Most likely I have a wrong idea of some concepts, so pls correct me if I'm talking nonsense.

如果这实际上是个好主意,可能需要在另一篇帖子中提出这个问题,但如果有更好/常见的方法来实现我正在尝试做的事情,请随时提供一些信息。很可能我对某些概念有错误的理解,所以如果我在说无聊的话,请纠正我。

In any case, thank you very much.

无论如何,非常感谢。

英文:

I already have the backend with an API for my application and now wanted to add the frontend with SvelteKit. Since I dont want my API to be available on the public internet I thought it would be a good idea to deploy the server in the same network as the API and only access it from server side. Now I'm stuck on how to exactly do that.

I am already using the server load function which works perfectly fine when the page is first loaded, but isnt suitable to load more data on a button press (as far as I understand).

Another possibility which I am aware of are form actions to post data, but I haven't been able to make them work with a 3rd party library.

A more universal approach seems to be the handle server hook. Is it suitable for my usecase? And how does an example request triggering the hook look like? In the docs only the example code for the server.js is given and it's not clear to me which requests will be caught there.

If this is actually a good idea might be a question for another post but if there are better / common approaches to achieve exactly what I am trying to do, feel free to leave some information. Most likely I have a wrong idea of some concepts, so pls correct me if I'm talking nonsense.

In any case, thank you very much.

答案1

得分: 1

I am already using the server load function which works perfectly fine when the page is first loaded, but isnt suitable to load more data on a button press (as far as I understand).

That is incorrect. You can e.g. add query parameters via goto (which will be set as part of the URL in the searchParams) or use invalidateAll to just trigger load again.

英文:

> I am already using the server load function which works perfectly fine when the page is first loaded, but isnt suitable to load more data on a button press (as far as I understand).

That is incorrect. You can e.g. add query parameters via goto (which will be set as part of the URL in the searchParams) or use invalidateAll to just trigger load again.

huangapple
  • 本文由 发表于 2023年7月6日 15:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定