我正在编写一个基于 NodeJS 的服务器端和客户端 JavaScript 应用程序。我有 Controller 、模型、 View 和演示者。我面临的问题是代码的某些部分只需要服务器端,某些部分需要客户端,有些则两者都需要。
例如, Controller 对我来说是纯粹的服务器端事物,因此它们不应该在客户端可用。另一方面,演示者是纯粹的客户端事物,因此应该在客户端中可用。
看看我目前的糟糕结构:
project\
project\public\index.js
project\public\images\
project\protected\controllers\
project\protected\models\
project\protected\views\
project\protected\presenters\
我面临的问题是公用文件夹是文档根目录,而 protected 文件夹位于文档根目录之外。我需要能够在客户端和服务器端使用 View 。因此,我的 View 无法受到保护。这同样适用于模型和许多其他事物。我也需要能够在客户端访问它们。
我开始认为我必须将整个结构放在文档根目录下,除了一些配置文件之外。这是我应该做的吗?这种方法有什么问题吗?我这么问是因为大多数 Web 框架(Django、Zend Framework)的工作方式都是框架位于文档根目录之外。
最佳答案
My github structure (已过时)
-- Main level
project\
-- Your main app. Keep light
project\app.js
-- All your configuration, development/production setups
project\app-configure
-- Your server-side controllers/routing. Keep light
project\controllers\
-- Any WebSocket specific code.
project\socket-io\
-- Server side test
project\test\
-- Your public folder. Client side can access these files.
-- Serve this folder as static content
project\public\
-- I keep my backbone collections here. Used on both server & client
project\public\collections
-- public css files
project\public\css
-- public js files. Including a main.js to bootstrap the router
project\public\js
-- public models used on both server & client.
project\public\models
-- client side router, used to router hashbang urls. Can use same routing
-- logic as the server. This is virtually a second set of controllers around
-- All your models
project\public\routers\
-- public tests. QUnit based
project\public\test\
-- View files
project\public\views
-- Templates used to render HTML. Used on client & server
project\public\views\templates
-- Backbone view files. Used to code up interaction, and business logic
-- This uses templates to render HTML and DOM events to handle interaction
project\public\views\backbone-views
这是基于 express和 backbone 。 Controller 是express-controllers public\routers 是使用 davis 的客户端路由
基本上,因为 MVC 在客户端和服务器之间被大量重用,所以唯一不公开的是服务器端测试和服务器端 Controller 。以及配置设置和任何基于套接字 io 的代码。
我的建议很简单,两者中使用的任何内容都放在 \public\
由于在客户端和服务器上重用 MVC 都是新事物,因此没有任何示例可供您查看。除了在 github 上寻找大型开源 Node.js 网站之外。
关于node.js - 如何在 NodeJS 中构建模型和 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469897/