我应该如何使用服务器端和客户端代码编写 Node.js Web 应用程序?
我计划编写一个 spin/backbone.js 风格的 Web 应用程序,它基本上只是将一个大型 application.js 文件传输到客户端的浏览器,该浏览器使用 ajax 与 node.js 后端进行通信。问题是我不知道如何构建这样的项目,因为我从未见过这样的应用程序的示例。我可以想象出不同方法的一些优点和缺点
将所有内容都保留在一个项目文件夹中。服务器端和客户端代码都驻留在同一文件夹中,这意味着它们可以共享表单输入验证和语言文件等资源。这似乎是一个很好的解决方案,但我不知道如何仅捆绑客户端需要的代码,而不是服务器代码。一般来说,我不知道如何实现这一点。如果以前已经完成过,我希望看到一些示例代码,甚至可能是 git 存储库
创建两个单独的项目。一种用于客户端,一种用于服务器。这看起来更简单、更直接,但在共享资源时就不那么优雅了。我必须编写两次表单输入验证等代码。
有什么想法吗?
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
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.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的第一种情况是一个非常棘手的情况,我建议我们还没有完全做到这一点。有些人会认为没有理由尝试实现这一目标,因为前端/后端总是会承担略有不同、有时甚至截然不同的任务。像 derby 这样的库显示出了希望,但还没有完全实现。
我最近与一位朋友讨论了这个问题,我们得出的结论是,目前最好的选择可能是通过 Websocket 序列化模型,然后确保节点服务器和客户端应用程序保持同步。
我可能会开发这样一个库,但目前我仍在使用 2 个文件夹和两侧的模型副本进行开发。布局标记从服务器发送,所有其他内容在从服务器接收 JSON 后呈现在客户端。坦率地说,重复的数量并没有那么大。有点烦人,但也保持了更大的灵活性,可以向不同方向生长。
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.
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.
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.
这不会是您问题的完整答案,但是如果您选择追求这样的努力,一个可能会有所帮助的库可能是 浏览器。
它的设计使您可以将类似的 require() 函数与包含许多不同模块的预处理或从模块源动态生成的 js 文件一起使用。这些模块可以通过相同的 require() 机制与服务器端共享。
我不知道在服务器端实现的骨干作为模型同步的服务器端对应部分的明确性,这似乎是您正在寻找的第一个目标,允许共享有意义的代码,例如模型和验证,以进行有用的共享。
另一件值得关注的事情是 requirejs,它使用更传统的脚本标签异步加载 f js 模块,但也可以在 Node.js 中工作,允许在节点和客户端代码之间共享相同的 AMD 模块。
This won't be a complete answer to your question, but one library that might help if you choose to pursue such an endeavour might be Browserify.
It's designed so you can use a similar require() function with a preprocessed, or on-the-fly generated from module source, js file containing many different modules. These modules can be shared with the server side through the same require() mechanism.
I don't know explicity of a Backbone implemented on the server side as a server side counter part for model sync, that would seem to be the first goal you are looking for, aloowing code that makes sense to be shared, such as models and validation, to be usefully shared.
Another thing to look at is requirejs, which uses more traditional script tag asynchronous loading f js modules, but also works within node.js aloowing the same AMD modules to a be shared between node and client code.
需要实时吗?否则德比的方式可能有点太沉重了。 Express.js提出了一种将客户端js分离在公共文件夹中的结构,并提供了快速运行RESTful API的方法,然后您可以使用 application.js 访问它。
我想你也可以通过 eval() 将“经典”js 文件从公共加载到节点中。
Was realtime required? Otherwise the Derby approach might be a little too heavy. Express.js proposes a structure where client js is separated in public folder, and provides methods to get a quick RESTful API running, which you can then access with your application.js.
I guess you could load "classic" js files from public into node via eval() too.
现在事情已经向前发展了很多,像
browserify 影响的编码可以帮助我们轻松实现这一点
,服务器和客户端之间总会有一些不常见的代码,但目标始终是将所有逻辑代码保留在不同的模块中(这些模块是稍后在这两个环境中使用)。从 TDD 的角度来看,这也更好,也可以减少键盘按下次数。
看看像这样的堆栈 -
http://mindthecode.com/lets-build- an-angularjs-app-with-browserify-and-gulp/
话虽如此,如果您有正确的编码人员编写正确的代码,那么您的 option1 对我来说似乎不太容易管理。
Things have moved much ahead now, and things like
browserify influenced coding can help us achieve this easily
there will always be some uncommon code between server and client sides, But the goal shall always be to keep all the logic code in different modules(which are later used from both environments). This is better from TDD point of view as well, also keeps your keyboard press count to lesser.
Have a look at things like this stack -
http://mindthecode.com/lets-build-an-angularjs-app-with-browserify-and-gulp/
Having said that your option1 did not seem that manageable to me, if you had the right coders coding the right code.