如何正确使用客户端和服务器端javascript文件- Node.js

时间:2022-06-20 15:59:57

This is very new to me, so apologies if these are obvious questions:

这对我来说是全新的,所以如果这些问题很明显,我很抱歉:

I would like a button on a web page to run a server side function and return the output to the client, would the following be the correct method?

我希望web页面上有一个按钮来运行服务器端函数并将输出返回给客户端,下面的方法是正确的吗?

  1. Button calls client side function
  2. 按钮调用客户端函数
  3. Client side function makes an AJAX request to a .js file on the server, which runs the function specified in the AJAX request.
  4. 客户端函数向服务器上的.js文件发出AJAX请求,该文件运行AJAX请求中指定的函数。
  5. The result of the server side function is sent back to the client and can be used to modify the web page content.
  6. 服务器端函数的结果被发送回客户端,可以用来修改web页面内容。

If this is the correct method, what would the (vanilla, preferably) AJAX request look like?

如果这是正确的方法,AJAX请求(最好是普通的)应该是什么样子?

My final question is, are only scripts listed within the HTML documents of the webpages downloaded to the client?

我的最后一个问题是,是否只有在网页的HTML文档中列出的脚本被下载到客户端?

I'm sure these are both simple questions, but thank you nonetheless.

我相信这些都是简单的问题,但还是谢谢你。

2 个解决方案

#1


2  

Yes that is mostly the correct flow for doing what you want. It is usually the case that your AJAX request will go to an api endpoint on the server, but it could also get the contents of a file.

是的,这是做你想做的事情的正确流程。通常情况下,AJAX请求将转到服务器上的api端点,但它也可以获取文件的内容。

Your ajax call will look something like:

您的ajax调用将如下所示:

$("button").click(function(){
    $.ajax({url: "/some/api/endpoint", success: function(result){
        $("#div1").html(result);
    }});
});

This uses jQuery to make the request.

这使用jQuery发出请求。

Regarding your second question, the browser can use scripts from .js files that are not embedded into your html

关于第二个问题,浏览器可以使用未嵌入到html中的.js文件中的脚本

#2


1  

Yes, this is the method i used for my RPI truck, the code would look something like this, if you use express and jquery:

是的,这是我用在RPI卡车上的方法,如果你用express和jquery,代码会是这样的:

client:

客户:

document.querySelector("#urdiv").innerHTML = $.ajax({
  type: "POST",
  url:"192.168.0.7/control/",
  data: {data: data}
})

server:

服务器:

app.post('/control/', function (req, res) {
  res.send("<h1>Your html goes here</h1>")
});

#1


2  

Yes that is mostly the correct flow for doing what you want. It is usually the case that your AJAX request will go to an api endpoint on the server, but it could also get the contents of a file.

是的,这是做你想做的事情的正确流程。通常情况下,AJAX请求将转到服务器上的api端点,但它也可以获取文件的内容。

Your ajax call will look something like:

您的ajax调用将如下所示:

$("button").click(function(){
    $.ajax({url: "/some/api/endpoint", success: function(result){
        $("#div1").html(result);
    }});
});

This uses jQuery to make the request.

这使用jQuery发出请求。

Regarding your second question, the browser can use scripts from .js files that are not embedded into your html

关于第二个问题,浏览器可以使用未嵌入到html中的.js文件中的脚本

#2


1  

Yes, this is the method i used for my RPI truck, the code would look something like this, if you use express and jquery:

是的,这是我用在RPI卡车上的方法,如果你用express和jquery,代码会是这样的:

client:

客户:

document.querySelector("#urdiv").innerHTML = $.ajax({
  type: "POST",
  url:"192.168.0.7/control/",
  data: {data: data}
})

server:

服务器:

app.post('/control/', function (req, res) {
  res.send("<h1>Your html goes here</h1>")
});