我正在用JavaScript客户端(在浏览器中运行)和Node.js服务器创建一个小应用程序,使用WebSocket通信。

我想在客户机和服务器之间共享代码。我才刚刚开始使用Node.js,至少可以说,我对现代JavaScript的知识有点生疏。因此,我仍然对CommonJS的require()函数感到困惑。如果我通过使用“export”对象创建我的包,那么我无法看到我如何在浏览器中使用相同的JavaScript文件。

我想创建一组在两端使用的方法和类,以方便编码和解码消息以及其他镜像任务。然而,Node.js/CommonJS打包系统似乎阻止了我创建可以在双方使用的JavaScript文件。

我还尝试使用JS.Class来获得更紧密的OO模型,但我放弃了,因为我不知道如何让提供的JavaScript文件与require()一起工作。我是不是遗漏了什么?


当前回答

如果你使用模块捆绑器(如webpack)来捆绑JavaScript文件以便在浏览器中使用,你可以简单地重用你的Node.js模块用于在浏览器中运行的前端。换句话说,你的Node.js模块可以在Node.js和浏览器之间共享。

例如,你有下面的代码sum.js:

普通Node.js模块:sum.js

const sum = (a, b) => {
    return a + b
}

module.exports = sum

使用Node.js中的模块

const sum = require('path-to-sum.js')
console.log('Sum of 2 and 5: ', sum(2, 5)) // Sum of 2 and 5:  7

在前端重用它

import sum from 'path-to-sum.js'
console.log('Sum of 2 and 5: ', sum(2, 5)) // Sum of 2 and 5:  7

其他回答

如果你想用类似node .js的风格编写浏览器,你可以尝试二元化。

没有浏览器代码编译,因此您可以不受限制地编写应用程序。

在浏览器的Node.js模块模式、AMD模块模式和全局模式中检查jQuery源代码:

(function(window){
    var jQuery = 'blah';

    if (typeof module === "object" && module && typeof module.exports === "object") {

        // Expose jQuery as module.exports in loaders that implement the Node
        // module pattern (including browserify). Do not create the global, since
        // the user will be storing it themselves locally, and globals are frowned
        // upon in the Node module world.
        module.exports = jQuery;
    }
    else {
        // Otherwise expose jQuery to the global object as usual
        window.jQuery = window.$ = jQuery;

        // Register as a named AMD module, since jQuery can be concatenated with other
        // files that may use define, but not via a proper concatenation script that
        // understands anonymous AMD modules. A named AMD is safest and most robust
        // way to register. Lowercase jquery is used because AMD module names are
        // derived from file names, and jQuery is normally delivered in a lowercase
        // file name. Do this after creating the global so that if an AMD module wants
        // to call noConflict to hide this version of jQuery, it will work.
        if (typeof define === "function" && define.amd) {
            define("jquery", [], function () { return jQuery; });
        }
    }
})(this)

也许这不完全符合你的问题,但我还是想分享一下。

我想在string上声明几个简单的string实用函数。原型,可用于节点和浏览器。我只是把这些函数保存在一个名为utilities.js的文件中(在子文件夹中),并且可以很容易地从我的浏览器代码中的脚本标记中引用它,并在我的Node.js脚本中使用require(省略.js扩展名):

my_node_script.js

var utilities = require('./static/js/utilities')

my_browser_code.html

<script src="/static/js/utilities.js"></script>

我希望这些信息对别人有用,而不是对我。

服务器可以简单地将JavaScript源文件发送到客户端(浏览器),但诀窍是客户端必须提供一个迷你的“导出”环境,然后才能执行代码并将其存储为模块。

创建这样一个环境的一个简单方法是使用闭包。例如,假设您的服务器通过HTTP(如http://example.com/js/foo.js)提供源文件。浏览器可以通过XMLHttpRequest加载所需的文件,并像这样加载代码:

ajaxRequest({
  method: 'GET',
  url: 'http://example.com/js/foo.js',
  onSuccess: function(xhr) {
    var pre = '(function(){var exports={};'
      , post = ';return exports;})()';
    window.fooModule = eval(pre + xhr.responseText + post);
  }
});

关键是客户端可以将外部代码包装到一个匿名函数中立即运行(一个闭包),该函数创建“exports”对象并返回它,这样你就可以将它分配到你想要的地方,而不是污染全局命名空间。在这个例子中,它被分配给窗口属性fooModule,该属性将包含文件foo.js导出的代码。

如果你使用模块捆绑器(如webpack)来捆绑JavaScript文件以便在浏览器中使用,你可以简单地重用你的Node.js模块用于在浏览器中运行的前端。换句话说,你的Node.js模块可以在Node.js和浏览器之间共享。

例如,你有下面的代码sum.js:

普通Node.js模块:sum.js

const sum = (a, b) => {
    return a + b
}

module.exports = sum

使用Node.js中的模块

const sum = require('path-to-sum.js')
console.log('Sum of 2 and 5: ', sum(2, 5)) // Sum of 2 and 5:  7

在前端重用它

import sum from 'path-to-sum.js'
console.log('Sum of 2 and 5: ', sum(2, 5)) // Sum of 2 and 5:  7