简体   繁体   English

使用 Node.JS、EJS 和前端 JS 构建可重用代码的最佳方式?

[英]Best way to structure reusable code using Node.JS, EJS, and front end JS?

Evidently the 'Software Engineering' exchange was the wrong place to ask this:显然,“软件工程”交流是问这个问题的错误地方:

I'm more or less learning the MEAN stack (have yet to start on Angular, so currently using straight vanilla JS for front-end) and part of what I'm building for my portfolio is a drag-and-drop form builder.我或多或少地在学习 MEAN 堆栈(尚未开始使用 Angular,因此目前使用纯香草 JS 作为前端)并且我为我的投资组合构建的部分内容是拖放表单构建器。

Currently the form has sections, which contain questions, which can contain multiple options as well as multiple follow up/sub questions (which can then contain their own options, but not follow ups).目前,该表单具有包含问题的部分,其中可以包含多个选项以及多个后续/子问题(然后可以包含自己的选项,但不包含后续问题)。

EJS helps me with the original render of the stored form using nested for..of loops, however my question comes into play when adding new elements to the form. EJS 使用嵌套的for..of循环帮助我对存储的表单进行原始渲染,但是当向表单添加新元素时,我的问题开始发挥作用。

Currently, I have vanilla front-end JS that is looking at some template tags within the page, then filling those in for new sections, questions, and options.目前,我有一个普通的前端 JS,它正在查看页面中的一些template标签,然后为新的部分、问题和选项填充这些标签。

However it doesn't seem very DRY because I'm using essentially the same logic in EJS when initially rendering the page (albeit multiple times).然而,它似乎不是很干,因为我在最初呈现页面时(尽管多次)在 EJS 中使用了基本相同的逻辑。

To make my code more reusable, should I write functions on the back end which are cast into the EJS render call both for the initial render and then available to the front-end JS, or cast the EJS variable containing the form (from MongoDB) into the front-end JS directly and use functions in there to both draw the page initially, as well as add new elements ?为了使我的代码更可重用,我应该在后端编写函数,这些函数被转换为初始渲染的 EJS 渲染调用,然后可用于前端 JS,还是转换包含表单的 EJS 变量(来自 MongoDB)直接进入前端 JS 并使用其中的函数来初始绘制页面,以及添加新元素? Both of these, hopefully, would make use of template tags in the HTML.希望这两个都可以使用 HTML 中的template标签。 Is one faster and/or safer than the other?一个比另一个更快和/或更安全吗?

Another option could also be to use EJS partials for sections, questions, and options to render the page, but I wouldn't know how to incorporate that into the front-end JS to add new elements without using template s, which is essentially what I'm doing now.另一种选择也可能是使用 EJS 部分,问题和选项来呈现页面,但我不知道如何将其合并到前端 JS 以添加新元素而不使用template ,这本质上是什么我现在正在做。

I'm going through the same ordeal right now.我现在正在经历同样的磨难。

From my understanding, your scenario requires using both the server side rendering (using EJS) and front end rendering using HTML templates and you want to know what's the best approach to go about it.据我了解,您的方案需要使用服务器端渲染(使用 EJS)和使用 HTML 模板的前端渲染,并且您想知道 go 的最佳方法是什么。

The short answer: cast everything on the front end and do the heavy lifting there.简短的回答:把所有东西都放在前端,然后在那里做繁重的工作。

I've used EJS components (includes) and they are simply not designed to be interactive.我使用了 EJS 组件(包含),它们根本不是为了交互而设计的。 Data flow is basically one way and there is no state management that I know of.数据流基本上是一种方式,我所知道的没有 state 管理。

You don't have to do this for the entire project mind you, this can be done for certain pages only where interactivity is very heavy.您不必为整个项目执行此操作,请注意,仅可以在交互性非常重的某些页面上执行此操作。

Just import Vue or any other SPA framework on the said page and act as if it is a single page App.只需在所述页面上导入 Vue 或任何其他 SPA 框架,就好像它是一个单页应用程序一样。 That way you don't miss out on server side benefits when you need them on other pages of the project.这样,当您在项目的其他页面上需要它们时,您就不会错过服务器端的好处。

Of course going into complete code split (API/SPA) would be the more traditional way but I'm sure there is a reason why you're forced to use server side rendering.当然,进入完整的代码拆分 (API/SPA) 将是更传统的方式,但我确信您被迫使用服务器端渲染是有原因的。

It helps that both (server/front end) use JavaScript which might not lead directly to code sharing but will surely make it easier to write the needed functions in similar ways on the back end and front end.两者(服务器/前端)都使用 JavaScript 会有所帮助,这可能不会直接导致代码共享,但肯定会更容易在后端和前端以类似的方式编写所需的函数。

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

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