简体   繁体   English

包括带有html和node.js的js文件

[英]including js files with html and node.js

I am performing messaging via websockets between a HTML5 client and server running on node.js. 我正在通过websot在HTML5客户端和node.js上运行的服务器之间执行消息传递。 Naturally I chose JSON as the message format and as such created common javascript code, defining the various message content types and transformation operations. 当然,我选择JSON作为消息格式,因此创建了常见的javascript代码,定义了各种消息内容类型和转换操作。 The javascript code is shared between both projects. javascript代码在两个项目之间共享。

I created my web client as one git project and my server as another git project. 我创建了我的Web客户端作为一个git项目和我的服务器作为另一个git项目。 Partly because I am using phonegap to build a webkit based client for various touch based environments. 部分是因为我使用phonegap为各种基于触摸的环境构建基于webkit的客户端。 It's also a nice separation of the various logic. 它也是各种逻辑的一个很好的分离。

To share the common code I created a separate project for the common logic and used git's subprojects to 'import' the code into the other two projects. 为了共享公共代码,我为公共逻辑创建了一个单独的项目,并使用git的子项目将代码“导入”到另外两个项目中。

Now this works fine for the html5 based project, as I can just do the following to include the code: 现在这适用于基于html5的项目,因为我可以执行以下操作来包含代码:

<script src="common/js/comms.js" type="text/javascript"></script>

However with node I've had problems trying to get the code. 但是对于节点,我在尝试获取代码时遇到了问题。 to get the code, I've ended up doing the following: 为了获得代码,我最终做了以下事情:

var fs = require('fs');
eval(fs.readFileSync('./common/js/comms.js').toString());

While the approach I have taken works, I've noticed that it's starting to get very messy when I have dependencies (as in, I need x.js, y.js and x.js if I want a.js), and I have to do it for every single node.js js file that wishes to use any of these entities. 虽然我采取的方法有效,但我注意到,当我有依赖关系时,它开始变得非常混乱(例如,如果我想要a.js,我需要x.js,y.js和x.js),而我必须为希望使用任何这些实体的每个node.js js文件执行此操作。

I'm also not comfortable using the eval approach. 我也不习惯使用eval方法。 I don't have a security issue with it, though I would like to use strict mode and it's my understanding that eval and strict mode go together like oil and water. 我没有安全问题,虽然我想使用严格模式,我的理解是eval和严格的模式像石油和水一样。

So my question is, what is the best method to include shared js files between html projects and node.js projects? 所以我的问题是,在html项目和node.js项目之间包含共享js文件的最佳方法是什么? I would prefer something that follows strict. 我更喜欢严格遵守的事情。

I should note that while there are several questions that are kinda around this topic, I could not find any that address the specific issues I'm raising. 我应该注意到,尽管围绕这个主题有几个问题,我找不到任何解决我提出的具体问题的问题。 I should also add that I do not wish to 'serve' the files from the 'server'. 我还要补充一点,我不希望“服务”来自'服务器'的文件。 The HTML5 client is to be 'standalone'. HTML5客户端应该是“独立的”。


To clarify, what I have in the 'common js files' is something like the following: 为了澄清,我在'common js files'中的内容如下:

var Comms = function (options) {
   ...
}

In HTML5 I can then just reference is via new Comms() , which is what I desire to do in node.js as well. 在HTML5中,我可以通过new Comms()引用,这也是我想在node.js中做的事情。

Have you researched how Node modules work? 您是否研究过Node模块的工作原理? If you're developing using this pattern, it's fairly simple to use require('./common/js/comms') on the server while still including it on your client as well. 如果您正在使用此模式进行开发,则在服务器上使用require('./common/js/comms')相当简单,同时仍将其包含在您的客户端上。

This article should point you in the right direction: https://caolan.org/posts/writing_for_node_and_the_browser.html 本文应指出正确的方向: https//caolan.org/posts/writing_for_node_and_the_browser.html


The following is the code that Tyler linked to in his comments below. 以下是Tyler在下面的评论中链接的代码。

The example (example.js): 示例(example.js):

if(typeof exports == "undefined"){
    exports = this;
}

Example = function() {
    this.init();
};

Example.prototype = {
    init: function() {
         console.log('hello world');
    }
};

exports.Example = new Example();

The node.js usage of example.js (app.js): example.js(app.js)的node.js用法:

example = require('./example');

The html usage of example.js (index.html): example.js(index.html)的html用法:

<html>
    <head>
        <title></title>
    </head>
    <body>
        <script src="./example.js"></script>
    </body>

The change by the OP was to assign exports.Example to Example instead of to an new instance. OP的更改是将exports.Example分配给Example而不是新实例。 Thus the node.js logic can use the following: 因此node.js逻辑可以使用以下内容:

var Example = require('./example.js').Example;
var foo = new Example();

Thus the original question is solved. 这样原来的问题就解决了。

Take a look at nodeJS modules . 看看nodeJS模块

Would be in comm.js: 将在comm.js:

module.exports = function(/* any dependency? */){
   // things
   // you can return some object
}

In the files you need comm.js 在文件中你需要comm.js

require('./common/js/comm.js')();
// or you can assign it to a variable if it returned something

Hope that helps! 希望有所帮助!

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

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