简体   繁体   English

node.js 在客户端和服务器之间共享 javascript 文件

[英]node.js sharing javascript file between client and server

I want a JavaScript file to be included in my client as well as server.我希望在我的客户端和服务器中包含一个 JavaScript 文件。

Having some issues.有一些问题。 Please help me.请帮我。

Code:代码:

require('app.js');

or或者

<script src='app.js'></script>

Which one should I use?我应该使用哪一种?

You can use Browserify to bundle your node module then import that js file with:您可以使用 Browserify 捆绑您的节点模块,然后使用以下命令导入该 js 文件:

<script src='app.js'></script>

The require() function will now be on the global namespace and you can call the following to make the module available to use: require()函数现在将位于全局命名空间中,您可以调用以下命令以使模块可供使用:

var app = require('app.js');

Note: in Browserify you'll need to export the module you want to import using require.注意:在 Browserify 中,您需要使用 require 导出要导入的模块。

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

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