简体   繁体   中英

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:

Uncaught ReferenceError: require is not defined

My html is the next:

<!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

    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.

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 . However be warned that it's only Google Chrome that uses that API. Also that API has a webkit prefix, so it could change if it ever becomes stable.

Additionally what you could do is have your game be made through Electron JS . Electron JS exposes the file system through NodeJS APIs, so your code wouldn't change. 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. The File System API as provided by Node is limited to Node.

PS It seems like you really want to use some functions that are really server specific too. I would recommend leaving the logs and emailing to the server. You could try out Heroku or Firebase for a good free tier service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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