与服务器协作
Working with the Server
上一节描述了服务器端渲染的过程,即使用服务器生成页面的 HTML 版本,该页面随后在浏览器中变得可交互。到目前为止,一切都是“同构”的;换句话说,你的应用在客户端和服务器上具有“相同(iso)的形状(morphe)”。
The previous section described the process of server-side rendering, using the server to generate an HTML version of the page that will become interactive in the browser. So far, everything has been “isomorphic”; in other words, your app has had the “same (iso) shape (morphe)” on the client and the server.
但服务器能做的不不仅仅是渲染 HTML!事实上,服务器可以做很多浏览器 做不到 的事情,比如从 SQL 数据库中读取和写入数据。
But a server can do a lot more than just render HTML! In fact, a server can do a whole bunch of things your browser can’t, like reading from and writing to a SQL database.
如果你习惯于构建 JavaScript 前端应用,你可能习惯于调用某种 REST API 来执行这类服务器工作。如果你习惯于使用 PHP、Python、Ruby(或 Java、C#……)构建网站,那么这种服务器端工作就是你的拿手好戏,而客户端交互往往只是事后才想到的。
If you’re used to building JavaScript frontend apps, you’re probably used to calling out to some kind of REST API to do this sort of server work. If you’re used to building sites with PHP or Python or Ruby (or Java or C# or...), this server-side work is your bread and butter, and it’s the client-side interactivity that tends to be an afterthought.
有了 Leptos,你可以同时做到这两点:不仅使用相同的语言,不仅共享相同的类型,甚至在同一个文件中!
With Leptos, you can do both: not only in the same language, not only sharing the same types, but even in the same files!
本节将讨论如何构建应用程序中独特的服务器端部分。
This section will talk about how to build the uniquely-server-side parts of your application.