我应该如何编写带有服务器端和客户端代码的node.js Web应用程序? [英] How should I go about writing a node.js web application with both server and client side code?

查看:142
本文介绍了我应该如何编写带有服务器端和客户端代码的node.js Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算编写一个spine / backbone.js样式的web应用程序,它基本上只是将一个大的application.js文件传输到客户端的浏览器,该浏览器使用ajax与node.js后端进行通信。问题是我不知道如何构建这样一个项目,因为我从未见过这样一个应用程序的例子。我可以通过不同的方式来描述一些优点和缺点

I'm planning on writing a spine/backbone.js style web application which basically just transfers a large application.js file to the client's browser that communicates with the node.js backend using ajax. The problem is that I don't know how to structure such a project, since I've never seen examples of such an application. I can picture some pros and cons with different ways of doing this


  • 将所有内容保存在一个项目文件夹中。服务器端和客户端代码都驻留在相同的文件夹中,这意味着它们可以共享资源,例如表单输入验证和语言文件。这似乎是一个很好的解决方案,但我不知道如何只捆绑客户端需要的代码,而不是服务器代码。总的来说,我不知道如何做到这一点。如果之前已经完成,我想看一些示例代码,甚至可能是git repo

  • Keep everything in one project folder. Both the server side and client side code resides in the same folders which means they can share resources such as form input validation and language files. This seems like a good solution, but I have no clue how I would bundle only the code that the client needs, and not the server code. Just in general I don't know how to accomplish this. If it has been done before, I would like to see some sample code, perhaps even a git repo

创建两个单独的项目。一个用于客户端,一个用于服务器。这似乎更简单直接,但在共享资源方面却不那么优雅。我必须两次编写表单输入验证等代码。

Create two separate projects. One for the client and one for the server. This seems a lot more simple and straight forward, but not as elegant when it comes to sharing resources. I would have to write code such as form input validation twice.

有什么想法吗?

推荐答案

你的第一种情况是一个非常棘手的情况,我建议我们还没有到那里。有些人认为没有理由尝试到达那里,因为前端/后端总是会有轻微的,有时甚至是截然不同的任务。像德比这样的图书馆显示出了希望,但还没有完全实现。

Your first situation is a very tricky scenario and I would suggest that we're not quite there yet. Some would argue that there's little reason to try to get there, as front/back ends will always be tasked with slightly and sometimes drastically different tasks. Libraries like derby show promise, but aren't quite there yet.

我最近和一位朋友讨论了这个问题,我们得出结论,现在最好的选择可能是在websockets上序列化模型,然后确保节点服务器和客户端应用程序保持同步。

I discussed this recently with a friend and we came to the conclusion that perhaps the best bet for now would be to serialize models over websockets, and then ensure that the node server and client app stay in sync.

我可能会在这样的库上工作,但是现在我仍然在开发两个文件夹和模型副本。布局标记从服务器发送,所有其他内容在从服务器接收JSON后呈现在客户端。坦率地说,重复的数量并不是那么重要。有点刺激,但也保持更大的灵活性,可以在不同的方向生长。

I may work on such a library, but for now I'm still developing with 2 folders and copies of models on both sides. Layout mark-up gets sent from the server, with all other content rendered client-side after receiving JSON from the server. Frankly, the amount of duplication isn't really that substantial. A little irritating but also maintains greater flexibility to grow in different directions.

这篇关于我应该如何编写带有服务器端和客户端代码的node.js Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆