简体   繁体   English

如何在Node.js和Web上同时使用JavaScript?

[英]How to use javascript on nodejs and web at the same time?

i want to use a config file in nodejs and in a web javascript. 我想在nodejs和网络javascript中使用配置文件。

config.js: config.js:

var conf = {};

conf.name = 'testname';
conf.pass = 'abc123';
conf.ip = '0.0.0.0';
conf.port = 100;
conf.delay = 5;

exports.config = conf;

use it in nodejs with: 在nodejs中使用它:

var conf = require('config.js');
console.log(conf.config.name);

want to use this same file inside html but how? 想要在html中使用同一文件,但如何? I was thinking by this way but i don't know how to use it in web. 我在想这种方式,但我不知道如何在网络上使用它。 When i try to use it in web i get Reference error: exports is not defined. 当我尝试在Web中使用它时,出现参考错误:未定义导出。

config.html: config.html:

<!doctype html>
<html lang="en">
<head>
    <title>Document</title>
    <script src="./config.js"></script>
    <script>
        var cnf = conf;
        function getCnf(){
            alert(cnf.config.name);
        }
    </script>
</head>
<body>
    <button onclick="getCnf();">test</button>
</body>
</html>

Anyone know how i must change config.js to use it in both systems nodejs and web? 有谁知道我必须如何更改config.js才能在系统nodejs和Web中使用它?

PS: Webside is running on nodejs http npm module. PS:Webside在nodejs http npm模块上运行。

You can put a condition around that, like this 您可以像这样放置一个条件

if (typeof module !== 'undefined' && module.exports) {
    module.exports.config = conf;
}

This makes sure that you have module and exports are available before setting any value on exports . 这可确保在设置exports值之前,您具有moduleexports功能可用。

Note: exports is just another variable referring module.exports . 注: exports是另一个变量引用module.exports So, they both are one and the same unless you assign something else to either of them. 因此,它们都是相同的,除非您为它们分配其他任何东西。 In case, you assign something to either of them, whatever is there in module.exports will be exported in Node.js. 万一你给它们分配了任何东西,无论模块中有什么module.exports都会导出到Node.js中。 You can read more about exports in this blog post 您可以在此博客文章中了解有关exports更多信息

You can use browserify to bundle your CommonJS for the browser without resorting to environment switches. 您可以使用browserify捆绑您的浏览器CommonJS的,而不诉诸环境开关。

  1. Install browserify using npm i browserify -g 使用npm i browserify -g安装npm i browserify -g
  2. Bundle your config.js and export it for external use with the -r tag 捆绑您的config.js并使用-r标签将其导出以供外部使用

    browserify -r ./config.js -o bundle.js

  3. Include the bundle in your code and use it: 在您的代码中包含捆绑软件并使用它:

<!doctype html>
<html lang="en">
<head>
    <title>Document</title>
    <script src="./bundle.js"></script>
    <script>
        var cnf = require("./config.js");
        function getCnf(){
            alert(cnf.config.name);
        }
    </script>
</head>
<body>
    <button onclick="getCnf();">test</button>
</body>
</html>

Thanks, that typeof was all i needed. 谢谢,那是我需要的typeof。

@Phoenix: I know that there is a way to do that but that's not necessary. @凤凰:我知道有办法做到这一点,但这不是必需的。 The variable are only used for some ajax requests and deley timers later. 该变量仅在以后用于某些ajax请求和deley计时器。

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

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