Node.js和客户端共享相同的脚本 [英] Node.js and client sharing the same scripts

查看:146
本文介绍了Node.js和客户端共享相同的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Node.js的一个理论好处是可以在客户端和服务器之间共享相同的脚本。

One of the theoretical benefits from working with Node.js is the possibility to share the same scripts between clients and the server. That would make it possible to degrade the same functionality to the server if the client does not support javascript.

但是,Node.js require()可能会降低服务器的相同功能。 )方法自己工作。在您加载的脚本中,您可以向 exports 中添加以下内容:脚本:

However, the Node.js require() method works on it's own. In the script you load, you can add stuff to this or exports that will later be available in the object that fetched the script:

var stuff = require('stuff');
stuff.show();

在stuff.js中:

In stuff.js:

this.show = function() {
    return 'here is my stuff';
}



因此,当在客户端上重新使用此脚本时, .show()方法将被添加到窗口作用域。这不是我们想要的,而是我们想要添加到一个自定义的命名空间。

So, when re-using this script on the client, the .show() method will be added to the window scope. That is not what we want here, instead we would like to add it to a custom namespace.

我唯一的解决方案到目前为止是像(在stuff.js):

My only solution so far is something like (in stuff.js):

var ns = typeof exports == 'undefined' ? (function() {
    return window['stuff'] = {};
})() : exports;

ns.show = function() {
    return 'here is my stuff';
}

delete ns; // remove ns from the global scope

这个工作得很好,因为我可以调用 stuff.show()。但它看起来很古怪。我试图搜索解决方案,但node.js仍然是非常新的(甚至对我),所以有几个可靠的资源。有没有人有更好的想法如何解决这个?

This works quite well since I can call stuff.show() on the server and client. But it looks quirky. I tried searching for solutions but node.js is still very new (even to me) so there are few reliable resources. Does anyone have a better idea on how to solve this?

推荐答案

总之,如果你想重复使用脚本, t使用Node.js特定的东西,你必须用最低的共同点在这里,浏览器。

In short, if you want to re-use scripts don't use Node.js specific stuff you have to go with the lowest common denominator here, the Browser.

解决方案是:


  1. 过度使用并使用 RequireJS ,此将使它工作在Node.js和浏览器。但你需要在服务器端使用RequireJS格式,并且还需要插入一个即时转换脚本...

  1. Go overkill and use RequireJS, this will make it work in both Node.js and the Browser. But you need to use the RequireJS format on the server side and you also need to plug in an on the fly converted script...

自己的加载器


  1. 使用匿名函数将重复使用的脚本包含在服务器端和客户端

  2. 现在创建一些代码,用户调用(模块)在该函数上,在节点侧传递 this 模块,在客户端传递一个命名空间对象

  1. Wrap your re-use scripts both on the server and client side with an anonymous function
  2. Now create some code that users call(module) on that function, on the Node side you pass in this for the module, on the client side you pass in a name space object


  • 保持简单和愚蠢,并且不要在Node.js端的模块作用域中使用

  • Keep it simple and stupid, as it is now, and don't use this in the module scope on the Node.js side of things

    我希望我能给你一个简单的开箱即用的解决方案,但两种环境在这种情况下有很大不同。如果你真的有大量的代码,你可以考虑一个生成脚本生成文件。

    I wish I could give you a simple out of the box solution, but both environments differ to much in this case. If you really have huge amounts of code, you might consider a build script which generates the files.

    这篇关于Node.js和客户端共享相同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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