简体   繁体   English

如何将JavaScript变量传递给node.js

[英]How to pass a JavaScript variable to node.js

I'm trying to use some js variables to save them in a .log file, I'm trying to pass this variables to another file that saves the data in a .log file but I get the next error: 我正在尝试使用一些js变量将它们保存在.log文件中,我试图将这些变量传递给另一个将数据保存在.log文件中的文件,但是我得到了下一个错误:

Uncaught ReferenceError: require is not defined

My html is the next: 我的HTML是下一个:

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="style.css">
    <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src="java.js"></script>
    <title></title>
</head>

<body onload="draw();">

    <!-- JS dependencies -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Bootstrap 4 dependency -->
    <script src="https://unpkg.com/popper.js@1.15.0/dist/umd/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>

    <!-- bootbox code -->
    <script src="bootbox/bootbox.js"></script>
    <script src="bootbox/bootbox.locales.js"></script>

    <div class="container">
        <div class="row">
            <div id="Prueba" class="col-4 d-flex justify-content-center">
                <h1></h1>
                <!--<button type="button" class="btn btn-info">Info</button>-->
            </div>
            <div class="col-4 d-flex justify-content-center">
                <div class="col">
                    <div id="chart"></div>
                </div>
            </div>
            <div class="col-4 d-flex justify-content-center">
                <!--<button type="button" class="btn btn-info">Info</button>-->
            </div>
        </div>
    </div>
</body>

</html>

What I do is show a game I made there, then I create some modals with inputs to get some data (I'm using bootbox.js), I save that in a variable and I want to send that to another .js that saves the data in a .log file, so I have a function that loads with le html and in that function I create the game and the modals, I open the first modal when the page is loaded 我做的是显示我在那里制作的游戏,然后我创建一些带输入的模态以获取一些数据(我使用的是bootbox.js),我将其保存在一个变量中,我想将其发送给另一个节省的.js .log文件中的数据,所以我有一个用le html加载的函数,在该函数中我创建游戏和模态,我在页面加载时打开第一个模态

    modal1();

    function modal2() {
        var data = require('./save');
        bootbox.prompt({
            closeButton: false,
            title: "Código postal",
            size: "medium",
            inputType: "number",
            callback: function (result) {
                num = number(result);
                if (num != true || result.length < 5) {
                    modal2();
                } else {
                  data.save_cp(result);
                    bootbox.alert({
                        size: "small",
                        title: "Correcto",
                        message: ":O",
                        callback: function () {
                            /* your callback code */
                        }
                    });
                }
            }
        });
    }

    function modal1() {
        var data = require('./save');
        bootbox.prompt({
            closeButton: false,
            title: "Correo empresarial",
            size: "medium",
            //onScape: function(){},
            callback: function (result) {
                correo = email(result);
                empresarial = ce(result);
                if (correo != true || empresarial == true) {
                    modal1();
                } else {
                    data.save_c(result);
                    modal2();
                }
            }
        });
    }

And the error says that require is not defined, I'm guessing that is because I need node, so my question is How can I solve this problem without runing node?, or is there another way to save this in a log or txt file without download the file, just open it and add the data. 并且错误说需要没有定义,我猜这是因为我需要节点,所以我的问题是如何在不运行节点的情况下解决这个问题?或者是否有另一种方法将其保存在日志或txt文件中无需下载文件,只需打开它并添加数据即可。

Access to the file system from a browser isn't possible in a stable way. 无法以稳定的方式从浏览器访问文件系统。

If you really want to use files, what you could try doing is using the FileSystem API the Google Chrome provides . 如果您真的想要使用文件,那么您可以尝试使用Google Chrome提供FileSystem API However be warned that it's only Google Chrome that uses that API. 但是请注意,只有谷歌Chrome才使用该API。 Also that API has a webkit prefix, so it could change if it ever becomes stable. 此API也有一个webkit前缀,因此如果它变得稳定,它可能会改变。

Additionally what you could do is have your game be made through Electron JS . 此外,你可以做的是让你的游戏通过Electron JS制作 Electron JS exposes the file system through NodeJS APIs, so your code wouldn't change. Electron JS通过NodeJS API公开文件系统,因此您的代码不会改变。 However that won't be a website, but a desktop app that could be distributed from a website. 然而,这不是一个网站,而是一个可以从网站分发的桌面应用程序。

Other than that, the download trick would be the only way to access files from a browser. 除此之外,下载技巧将是从浏览器访问文件的唯一方法。

EDIT: I've tried using browserify on the file system apis before, but that doesn't work. 编辑:我之前尝试过在文件系统apis上使用browserify,但这不起作用。 The File System API as provided by Node is limited to Node. Node提供的文件系统API仅限于Node。

PS It seems like you really want to use some functions that are really server specific too. PS看起来你真的想要使用一些真正特定于服务器的功能。 I would recommend leaving the logs and emailing to the server. 我建议将日志和电子邮件留给服务器。 You could try out Heroku or Firebase for a good free tier service. 您可以尝试使用Heroku或Firebase获得良好的免费套餐服务。

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

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