包括带有html和node.js的js文件 [英] including js files with html and node.js

查看:137
本文介绍了包括带有html和node.js的js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过websot在HTML5客户端和node.js上运行的服务器之间执行消息传递。当然,我选择JSON作为消息格式,因此创建了常见的javascript代码,定义了各种消息内容类型和转换操作。 javascript代码在两个项目之间共享。

I am performing messaging via websockets between a HTML5 client and server running on 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. The javascript code is shared between both projects.

我将我的Web客户端创建为一个git项目,将我的服务器创建为另一个git项目。部分原因是我使用phonegap为各种基于触摸的环境构建基于webkit的客户端。这也是各种逻辑的一个很好的分离。

I created my web client as one git project and my server as another git project. Partly because I am using phonegap to build a webkit based client for various touch based environments. It's also a nice separation of the various logic.

为了共享公共代码,我为公共逻辑创建了一个单独的项目,并使用git的子项目将代码导入另外两个项目。

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.

现在这适用于基于html5的项目,因为我可以执行以下操作来包含代码:

Now this works fine for the html5 based project, as I can just do the following to include the code:

<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());

虽然我采取的方法有效,但我注意到它开始变得非常凌乱有依赖关系(如我想要a.js,我需要x.js,y.js和x.js),我必须为希望使用这些实体中的任何一个的每个node.js js文件执行此操作。

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.

我也不习惯使用 eval 方法。我没有安全问题,虽然我想使用严格模式,我的理解是eval和严格模式像油和水一样。

I'm also not comfortable using the eval approach. 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.

所以我的问题是,在html项目和node.js项目之间包含共享js文件的最佳方法是什么?我更喜欢严格的事情。

So my question is, what is the best method to include shared js files between html projects and node.js projects? I would prefer something that follows strict.

我应该注意到虽然有几个问题有点围绕这个主题,我找不到任何解决具体问题我提高。我还要补充一点,我不希望服务来自'服务器'的文件。 HTML5客户端将是独立的。

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'.

为了澄清,我在常见的js文件中的内容是如下所示:

To clarify, what I have in the 'common js files' is something like the following:

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

在HTML5中我只能通过引用新的Comms(),这也是我想在node.js中做的事情。

In HTML5 I can then just reference is via new Comms(), which is what I desire to do in node.js as well.

推荐答案

您是否研究过Node模块的工作原理?如果您正在使用此模式进行开发,则在服务器上使用 require('./common/js/comms')相当简单,同时仍将其包含在您的客户端上好吧。

Have you researched how Node modules work? 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.

本文应指出正确的方向: https://caolan.org/posts/writing_for_node_and_the_browser.html

This article should point you in the right direction: https://caolan.org/posts/writing_for_node_and_the_browser.html

以下是Tyler在下面的评论中链接到的代码。

The following is the code that Tyler linked to in his comments below.

示例(example.js):

The example (example.js):

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

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

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

exports.Example = new Example();

example.js(app.js)的node.js用法:

The node.js usage of example.js (app.js):

example = require('./example');

example.js(index.html)的html用法:

The html usage of example.js (index.html):

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





OP的变化是将 exports.Example 分配给示例而不是新实例。因此node.js逻辑可以使用以下内容:

The change by the OP was to assign exports.Example to Example instead of to an new instance. Thus the node.js logic can use the following:

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

这样原来的问题就解决了。

Thus the original question is solved.

这篇关于包括带有html和node.js的js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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