在Node应用程序间共享代码 [英] Sharing code between Node applications

查看:78
本文介绍了在Node应用程序间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序由两部分组成:API和客户端。两者都是独立的Node应用程序尽管API除了文档之外还没有真正的前端组件,它们仍然共享一些常见文件,例如Jade组件,规范化CSS,实用程序模块,最重要的是Mongoose的模式& MongoDB交互的模型定义。我真的不想习惯于我首先对API进行更改的工作流程,然后将更改后的文件复制到客户端,反之亦然,所以找到一个可行的解决方案非常好。



跨Node应用程序共享各种代码的最佳方式是什么? 网络上,我遇到了一些解决方案,但在这种情况下,他们都没有真正的工作。

一种解决方案是将公共文件制作成节点模块,并使其与NPM应用程序保持同步,但这意味着我(以及未来可能的开发人员)必须知道哪些文件是公共模块的一部分,并要求它们使用 require('node_modules / mongo / schemas / example.js')而不是 require('mongo / schemas / example.js')例如。由于假设可能有数百个通用文件,因此不可能记住文件是否通用,特别是对于新开发者。

另一个解决方案是制作通用模块的git子模块。这将是完美的,只要我能够将子模块路径包含到Node的require路径中,以防万一在预期位置找不到所需的文件(例如 mongo / schemas / example.js ),它会查看公共子模块( common / mongo / schemas / example.js )。这会很棒,因为它会允许用本地版本覆盖一个公共文件。这有点像Magento的作品。这里的问题是,Node不允许编辑require路径(至少,据说在Node 0.5之前是可能的),所以这真是一件坏事。



第三个解决方案是将公共存储库的内容粗暴地合并到应用程序的根目录中。在这个解决方案中没有真正不可逾越的问题,但我觉得这是一种不好的做法。毕竟,它将无法通过应用程序编辑通用文件,因为无法将通用文件选择性地推送到通用的git存储库。所以基本上这个解决方案只会让工作流程变得更乏味:对公用文件的所有修改都必须手动复制到公共代码库,然后推送到git remote,然后复制到其他应用程序。所以这个解决方案绝对不会为我削减它。



看起来第二个解决方案是我唯一的(也是最好的)选项,除非你们出现了与一个更好的(我真的希望:D)。如果有人知道改变节点需求路径的方式,我将非常感激。

解决方案


一个解决方案是使公共文件中的节点模块保持同步与NPM之间的应用程序


这是唯一一个很好的解决方案。如果你有一个共享的代码,它应该是一个模块。


但是这意味着我(和可能的未来的开发者)必须知道哪些文件是公共模块的一部分,并且需要require('node_modules / mongo / schemas / example.js')而不是require('mongo / schemas / example.js'),例如


没有。它是 require('mongo / schemas / example.js')而不是 require('./ mongo / schemas / example.js') code>。或者更好的是, require('mongo')。Schemas.example ,尽管它需要一些工作。



你必须将您的代码分割成逻辑模块。我想这种噩梦会证明NODE_PATH的使用是正确的,但我真的建议重构这个...


Our app consists of two components: the API and the client. Both are independent Node applications. While the API doesn't really have any frontend components besides the docs, they both still share some common files, like Jade components, normalizing CSS, utility modules and most importantly Mongoose's schema & model definitions for MongoDB interaction. I really don't want to get used to a workflow where I first make changes to the API, and then copy the changed files to the client or vice versa, so it would be great to find a viable solution for this.

What would be the best way to share miscellaneous code across Node apps?

While browsing the web, I've come across a couple of solutions, but none of them really work in this case.

One solution is to make a node module out of the common files and keep it in sync between the apps with NPM, but that would mean I (and possible future devs) have to know which files are part of the common module, and require them with require('node_modules/mongo/schemas/example.js') instead of require('mongo/schemas/example.js') for example. Since hypothetically there might be hundreds of common files, it would be impossible to remember if a file is common or not, especially for new devs.

Another solution is to make a git submodule of the common module. This would be perfect, if only I could include the submodule path to Node's require paths, so in case it didn't find the required file in the expected location (say, mongo/schemas/example.js), it would look in the common submodule (common/mongo/schemas/example.js). This would be awesome, since it would allow overwriting a common file with a "local" version. It's kind of like Magento works. The problem here is that Node doesn't allow editing the require paths (anymore at least, supposedly it was possible prior to Node 0.5), so that's a bummer.

The third solution is to brutally merge the common repository's contents into the app's root. There are no truly insurmountable problems in this solution, but I just feel like it's a bad practice. After all, it would make it impossible to edit the common files through the app, since there's no way to selectively push the common files into the common git repository. So basically this solution would only make the workflow even more tedious: all modifications to the common files would have to be copied by hand to the common codebase, pushed to the git remote and then copied to the other app. So this solution definitely doesn't cut it for me either.

It seems that the second solution is the only (and best) option I have, unless you guys come up with a better one (which I'm really hoping for :D). If anyone knows a way to change node's require paths, I would be extremely grateful.

解决方案

One solution is to make a node module out of the common files and keep it in sync between the apps with NPM

It's the only one good solution. If you have a shared code, it should be a module.

but that would mean I (and possible future devs) have to know which files are part of the common module, and require them with require('node_modules/mongo/schemas/example.js') instead of require('mongo/schemas/example.js') for example

No. It's require('mongo/schemas/example.js') instead of require('./mongo/schemas/example.js'). Or better yet, require('mongo').Schemas.example, though it requires some work.

You have to split your codebase into a logical modules. I guess that kind of a nightmare you have there would justify use of NODE_PATH, but I'd really suggest to refactor that...

这篇关于在Node应用程序间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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