简体   繁体   English

什么是同构应用?

[英]What is an isomorphic application?

I have been reading multiple different articles about what Isomorphic application is, why this type of architecture is better and so forth.我一直在阅读多篇关于什么是同构应用程序、为什么这种类型的架构更好等等不同的文章。 But I still have a bit of uncertainty as to what is meant by such term.但我对这个术语的含义仍有一些不确定性。

How would you define what "Isomorphic Application" is, without going too much into details?您如何定义什么是“同构应用程序”,而又不涉及太多细节?

They are, more recently, also called universal .最近,它们也被称为universal I'm not sure about the whole app being called isomorphic/universal, but you can certainly have parts of the code base that is universal.我不确定整个应用程序是否被称为同构/通用,但您当然可以拥有通用的代码库部分。

Isomorphic/universal code is code that runs on both the client (browser) and on the server (NodeJS).同构/通用代码是在客户端(浏览器)和服务器(NodeJS)上运行的代码。 Since they are both JavaScript this something that is possible if:由于它们都是 JavaScript,因此在以下情况下是可能的:

  1. you do not mention window , document or any other browser-only methods您没有提到windowdocument或任何其他仅浏览器的方法
  2. you do not mention server , fs or any or any other node-only methods.您没有提到serverfs或任何或任何其他仅限节点的方法。
  3. If you do need to do the above within some code that is meant to be universal, you should wrap it in a function that either mocks the required method within the alternate environment or wrap it in conditionals so that the app doesn't crash.如果您确实需要在一些通用代码中执行上述操作,则应将其包装在一个函数中,该函数在备用环境中模拟所需的方法或将其包装在条件中,以便应用程序不会崩溃。

An example is console.log which will work both within NodeJS and any browser, along with most other es6 methods in modern browsers.一个例子是console.log ,它可以在 NodeJS 和任何浏览器中工作,以及在现代浏览器中的大多数其他 es6 方法。

I use build tools (like webpack) to then help create / export functions within individual files so that we then have a bundle like client-app.js which is included in the HTML file and is the browser only js.我使用构建工具(如 webpack)来帮助在单个文件中创建/导出函数,这样我们就有了一个像client-app.js这样的bundle ,它包含在 HTML 文件中,并且是浏览器唯一的 js。 The server then might start using server-app.js which is the server-only bundle.然后服务器可能会开始使用server-app.js ,它是仅限服务器的包。 Both bundles can be created using a lot of the same universal source code.可以使用许多相同的universal源代码创建这两个包。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM