我应该在电子应用程序中使用什么作为后端?

2022-01-10 00:00:00 electron javascript

我知道我可以使用 HTML CSS JavaScript 或任何其他前端框架来设计 UI.我什至明白我可以连接到任何远程 API.如果我想要一个带有数据库的独立应用程序怎么办.我应该如何连接到数据库?我应该在哪里写我的应用程序逻辑?我觉得电子中有一个缺失的部分我应该使用像 Express 这样的节点 JS Web 框架吗?或者我应该在不使用任何框架的情况下用纯节点 JS 编写所有程序逻辑?如果可能的话,编写电子应用程序的最佳方法是什么,请指出任何工作示例.

I understand I can design the UI with HTML CSS JavaScript or any other frontend framework. I even understand that I can connect to any remote API. what if I want a standalone application with database. How should I connect to the database ? where should I write my application logic? I feel like there is a missing part in electron do I supposed to use a node JS web Framework like Express? or I should write all the program logics in pure node JS without using any framework? What is the best approach to write electron applications if possible please point me any working example.

推荐答案

这完全取决于你.

虽然我们在 Web 应用程序中习惯的客户端/服务器(前端/后端)模型是一个好主意(关注点分离),但当客户端和服务器在同一台机器.

While the client/server (frontend/backend) model we've got used to in web applications is a good idea (separation of concerns), it's not the only way to do things when the client and the server are on the same machine.

Electron 构建在 Node.js 之上.因此,您可以使用通常的 npm 模块来连接到您想要使用的任何数据库系统并取消框架.例如,如果您愿意,您可以编写代码以将数据从数据库直接提取到您的 onclick 事件处理程序中.

Electron is built on top of Node.js. So you can use the usual npm modules in order to connect to whatever database system you want to use and do away with frameworks. For example you can write code to fetch data from the database right into your onclick event handler if you desire so.

话虽如此,如果您不小心,您很可能会发现自己正在处理一堆难以管理的意大利面条代码.因此,即使您不想要整个客户端/服务器系统, 种结构也是推荐的.

Having said that, odds are you will find yourself dealing with an unmanageable bunch of spaghetti code if you're not careful. So, some kind of structure is recommended even if you don't want an entire client/server system.

此外,您的客户端"和服务器"不必通过 HTTP 进行通信.接口可以只是普通的函数(和/或方法)调用.Electron 还有一个消息传递系统(例如:https://electronjs.org/docs/api/ipc-main 你可以使用.

Also, your "client" and your "server" don't have to communicate through HTTP. The interface can be just plain function (and/or method) calls. Electron also has a message passing system (for example: https://electronjs.org/docs/api/ipc-main that you may use.

相关文章