如何使用javascript命令运行node.js文件?

如何使用javascript命令运行node.js文件?,javascript,node.js,json,Javascript,Node.js,Json,我想做一个网络游戏。它不是快节奏的,所以我只需要一个json文件。我对node.js很陌生,对它了解不多 我尝试过使用这个:并且我了解如何使用命令行对其进行读写。如何从像OnClick这样的javascript事件运行节点js文件 节点js: //this code is mostly stolen from the site I linked to. 'use strict'; const fs = require('fs'); let newdata = {"newdata": "thi

我想做一个网络游戏。它不是快节奏的,所以我只需要一个json文件。我对node.js很陌生,对它了解不多

我尝试过使用这个:并且我了解如何使用命令行对其进行读写。如何从像OnClick这样的javascript事件运行节点js文件

节点js:

//this code is mostly stolen from the site I linked to.
'use strict';

const fs = require('fs');

let newdata = {"newdata": "this is new!"}  
let data = JSON.stringify(newdata);  
fs.writeFileSync('data.json', data);   
json:

{
   "test": "hello world"
}
html:

<input type="button" value="Click Me!" onclick="
//run the node js file!!!
">


我不知道我能做些什么来运行这个文件。

我认为在解决这个问题之前,你需要问问自己你想得到什么

  • 如果您想让服务器在REST API中发送JSON文件,使用NodeJS是一个好方法,但是您需要让web服务器在后台运行,并使用
    $。在HTML代码处使用get
    命令来获取数据

  • 如果您想在本地读取JSON文件,则不需要使用NodeJS(JS将是解决此问题的好方法)

  • 对于第一种解决方案:

    节点代码:

    const data = require('/path/to/json/file/data.json')
    var path = require('path');
    const express = require('express')
    const app = express()
    
    app.get('/data', (req, res) => {
        res.json(data)
    })
    
    app.get('/', function(req, res) {
        res.sendFile(path.join('/path/to/index/file/index.html'));
    });
    
    app.listen('8080')
    
    <!DOCTYPE html>
    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <body>
            <button onclick="myFunction()">Test</button>
        </body>
        <script>
            function myFunction(){
                $.get("http://localhost:8080/data",
                    function(data) {
                        alert(JSON.stringify(data));
                    }
                );
            }
        </script>
    </html>
    
    HTML代码:

    const data = require('/path/to/json/file/data.json')
    var path = require('path');
    const express = require('express')
    const app = express()
    
    app.get('/data', (req, res) => {
        res.json(data)
    })
    
    app.get('/', function(req, res) {
        res.sendFile(path.join('/path/to/index/file/index.html'));
    });
    
    app.listen('8080')
    
    <!DOCTYPE html>
    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <body>
            <button onclick="myFunction()">Test</button>
        </body>
        <script>
            function myFunction(){
                $.get("http://localhost:8080/data",
                    function(data) {
                        alert(JSON.stringify(data));
                    }
                );
            }
        </script>
    </html>
    
    
    测验
    函数myFunction(){
    $.get(”http://localhost:8080/data",
    功能(数据){
    警报(JSON.stringify(数据));
    }
    );
    }
    
    nodeJS文件托管在web服务器中,您只能使用http方法、get、post访问这些文件。您不能通过单击按钮来运行node.js文件。我肯定需要创建一个服务器。我想要的是有一个在线的服务器,输入类似于:server.player1.points的内容,并能够实时读写。我目前有一个域名,我还需要购买其他东西吗?@Ben我为你的问题添加了我的解决方案