简体   繁体   English

在AngularJS和Nodejs之间共享代码

[英]Sharing code between AngularJS and Nodejs

What is the best way of sharing code between frontend and backend using javascript , specifically saying between nodejs and angularjs ? 使用javascript在前端和后端之间共享代码的最佳方法是什么,特别是在nodejsangularjs之间说什么?

Thing is that we are using same enums and constant values such as error codes in both backend and frontend. 事实是我们使用相同的enumsconstant values例如后端和前端的error codes Right now we just copy&paste each change to both platform which isn't a good solution. 现在我们只是将每个更改复制并粘贴到两个平台上,这不是一个好的解决方案。 There are also some services that could be shared. 还有一些可以共享的服务。

I have seen libraries such as browserify ; 我见过像browserify这样的库; but thats not exactly what I am looking for. 但那不是我想要的。 I am looking for a solution similar to maven dependency in java . 我正在寻找类似于java maven dependency的解决方案。 In java , libraries can be shared easily using maven , whereas I can't find a similar way of doing that in javascript . java ,可以使用maven轻松共享库,而我在javascript找不到类似的方法。 Is there a way to isolate these services and give them as dependency to nodejs using npm and to angularjs using bower independently? 有没有一种方法来隔离这些服务,并给他们的依赖关系nodejs使用npmangularjs使用bower独立? Or what are the ways for sharing the same code between frontend and backend? 或者在前端和后端之间共享相同代码的方法是什么?

There are several ways to do this. 有几种方法可以做到这一点。 The first is that you can make a new package which is required via bower for the front-end code and via npm for the backend code. 首先,您可以通过bower为前端代码创建一个新包,并通过npm创建后端代码。 I have several packages published to both systems. 我有几个包发布到两个系统。

Install with Bower -- information on how to install modules that aren't in the registry 使用Bower安装 - 有关如何安装不在注册表中的模块的信息

NPM Install docs -- all the ways to install with npm (private github with auth: git+ssh://git@github.com/[org]/[repo] ) NPM安装文档 - 使用npm安装的所有方法(私有github与auth: git+ssh://git@github.com/[org]/[repo]

Just create a new module with your shared data and install it using both package managers. 只需使用共享数据创建一个新模块,然后使用两个包管理器进行安装。 Both of them allow you to install an unpublished module so if it's private data you can keep it as such. 它们都允许您安装未发布的模块,因此如果它是私有数据,您可以保留它。

If your front end requires require.js you can use something like amdefine to make it available to your node backend, or if you're just using legacy window code you can do something like: 如果您的前端需要require.js您可以使用amdefine东西使其可用于您的节点后端,或者如果您只是使用旧版窗口代码,则可以执行以下操作:

var mydata = {};

if(typeof window !== 'undefined'){
    window.mydata = mydata;
} else {
     module.exports = mydata;
}

If you are sharing a lot of data though I highly recommend looking into browserify to write your entire codebase in commonjs and using browserify to generate your client bundle. 如果您正在共享大量数据,我强烈建议您查看browserify以在commonjs中编写整个代码库,并使用browserify生成客户端包。 There is a laundry list of resources about using browserify, including stuff on how to use browserify and angular together 有一个关于使用browserify 的资源清单 ,包括如何使用browserify和angular一起使用的东西

Disclaimer - I'm still developing this approach and it's a little manual. 免责声明 - 我仍在开发这种方法,这是一本小手册。 I did this using npm, an npm cli called pac , and bower. 我使用npm,一个叫做pac的npm cli和凉亭做到了这一点。 Pac let's me avoid using npm install in production by keeping the modules as .tgz files (committed to project in source control). Pac让我避免在生产中使用npm install ,方法是将模块保存为.tgz文件(提交到源代码管理中的项目)。 With pac, when someone checks out the node project, they run pac install then npm rebuild instead of npm install . 使用pac,当有人检出节点项目时,他们运行pac install然后npm rebuild而不是npm install

My shared code is kept in a directory (my-module). 我的共享代码保存在一个目录(my-module)中。 It has both package.json and a bower.json. 它有package.json和bower.json。

My consuming node app has a package.json dependency for: "my-module" : "xyz" 我的消费节点应用程序有一个package.json依赖项:“my-module”:“xyz”

My consuming client has a bower.json dependency for: "my-module" : "../relative/path/to/my-module" 我的消费客户端有一个bower.json依赖项:“my-module”:“../ relative / path / to / my-module”

When I make updates to my-module, I update my node app by: 当我对my-module进行更新时,我通过以下方式更新我的节点应用:

  1. Making a tar.gz of the contents of my-module: tar -czvf my-module.tar.gz -C my-module 制作my-module内容的tar.gz: tar -czvf my-module.tar.gz -C my-module
  2. Removing the old version from the node app's node_modules 从节点应用程序的node_modules中删除旧版本
  3. Rerunning npm install path/to/my-module-tar.gz 重新运行npm install path/to/my-module-tar.gz
  4. Rerunning pac (this makes a .tgz of node_modules/my-module) 重新运行pac(这会生成一个.tgz的node_modules / my-module)
  5. Committing the updated pac .modules/my-module.tgz 提交更新的pac .modules / my-module.tgz

I update my client by: 我更新了我的客户:

  1. Removing the old client/bower_components/my-module 删除旧客户端/ bower_components / my-module
  2. Rerunning bower install or bower update 重新bower installbower update

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

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