一个标准的节能PHP的AJAX方案的node.js相当于节能、方案、标准、AJAX

2023-09-10 16:44:32 作者:要么留要么滚

在一个JavaScript(客户端)+ PHP(服务器)的网站,我有这个计划保存的在线记事本(在#文本框文本域)服务器:

  //客户端
$(#保存按钮)上(咔哒,保存)。

功能保存(五){
  $阿贾克斯({类型:POST,
           网址:/php/save.php
           数据:{项目名称:$('#项目名称)文本()。
                   数据:$('#文本框)文本()}。
           成功:功能(数据){警报(数据保存); },
           错误:功能(数据){的console.log(数据); }});
}
 

这在服务器端( /php/save.php ):

 < PHP

$项目名称= $ _ POST ['项目名称'];
$数据= $ _ POST ['数据'];

//保存此存入数据库

?>
 
js ajax通用方法,目前5个流行的AJAX调用JavaScript库

什么是相当于后到服务器并保存在服务器上方案中的Node.js(客户端+服务器)?(使用 EX preSS 为例)

有一个著名的node.js模式这样的任务呢?

它是常见的/很好的做法,使用 $。阿贾克斯(...)客户端和node.js的服务器?我想Node.js的推出一种新的思维方式,使得AJAX的不需要-了......我错了?

解决方案      什么是相当于后到服务器并保存在服务器上方案中的Node.js(客户端+服务器)? (使用前preSS为例)   

是特定于code,就应该找这样的事情。

客户端: -

  $(#保存按钮)上(咔哒,保存)。

功能保存(五){
  $阿贾克斯({类型:POST,
           网址:/ PHP /保存,
           数据:{项目名称:$('#项目名称)文本()。
                   数据:$('#文本框)文本()}。
           成功:功能(数据){警报(数据保存); },
           错误:功能(数据){的console.log(数据); }});
}
 

和服务器端: -

  router.post('/ PHP /保存,功能(REQ,RES){//服务器端
    VAR PROJECTNAME = req.body.projectname;
    VAR数据= req.body.data;
    //这里将数据保存到数据库
});
 

在这里,我收到节点后数据的方式,我需要需要身体解析器模块 所以这应该在顶部加入,

  VAR bodyParser =需要('身体解析器');
 

这将您解析HTTP请求的身体,所以你可以为req.body.data直接访问参数。

     是不是普通/好习惯使用$阿贾克斯(...)在客户端和node.js的服务器?   

$阿贾克斯(...)在客户端: - 中 - 当然,为什么不..不,我们使用 $阿贾克斯(...)当我们使用不同的后端技术,如.NET或Java?所以没有必要改变您的前端,它只是要在后端

使用不同的技术

在服务器上的节点: - 现在,这是基于相当的意见,节点是新的,我们的JavaScript开发人员喜爱的节点,因为它是在服务器端和客户端的单一技术,以及,我想说的节点是太棒了..但也有其他人谁也说,节点都是扯淡。我宁愿跳过回答这一部分。

     的node.js推出一种新的思维方式,使得AJAX的不需要-了...   

我想你是指向的WebSockets 。是的节点是一种思维方式不同。这并不能真正取代AJAX,你可以在这里找到的一些信息

的WebSockets的目的是提供一种低等待时间,双向,全双工和长期运行的浏览器和服务器之间的连接。的WebSockets开拓新的应用领域,以浏览器应用程序使用HTTP和AJAX(互动游戏,动态的媒体流,弥合现有的网络协议,等等),这些不是真的有可能。

如果你不需要说的WebSockets提供具体的好处,那么它可能是一个更好的主意,坚持如AJAX和彗星的现有技术,因为这可以让你重新使用,用的工具,技术,庞大的现有生态系统的整合,安全机制,知识基础(上计算器即更多的人知道HTTP / AJAX /彗星比的WebSockets),等等。

这是不是Node.js的具体的事情,你仍然可以实现在其他技术的WebSockets为好。

所以Node.js的推出一种新的思维方式,使得AJAX并不需要的,不再是 错了!的

In a JavaScript (client) + PHP (server) website, I have this scheme for saving an "online notepad" (in a #textbox textarea) to server:

// client-side
$("#save-button").on('click', save); 

function save(e) {
  $.ajax({ type: "POST",
           url: "/php/save.php",
           data: { projectname: $('#projectname').text(), 
                   data: $('#textbox').text() },
           success: function(data) { alert('Data saved'); },
           error: function(data) { console.log(data); } });
}

and this on server-side (/php/save.php):

<?php

$projectname = $_POST['projectname'];
$data = $_POST['data'];

// save this into database

?>

What is the equivalent "post to server and save on server" scheme in node.js (client+server)? (using express for example)

Is there a well-known node.js "pattern" for such task?

Is it common / good practice to use $.ajax(...) on client and node.js on server? I thought node.js introduced a new way of thinking, making ajax not-needed-anymore... am I wrong?

解决方案

What's the equivalent "post to server and save on server" scheme in node.js (client+server)? (using express for example)

being specific to your code, it should be looking something like this.

client-side:-

$("#save-button").on('click', save);

function save(e) {
  $.ajax({ type: "POST",
           url: "/php/save",
           data: { projectname: $('#projectname').text(), 
                   data: $('#textbox').text() },
           success: function(data) { alert('Data saved'); },
           error: function(data) { console.log(data); } });
}

And server side:-

router.post('/php/save', function(req, res) {   // server side
    var projectName = req.body.projectname;
    var data = req.body.data; 
    // save the data here to database
});

here the way I receive the post data in node, I need to require body-parser module so this should be added at the top,

var bodyParser = require('body-parser');

this will parse your http request body so you can access the parameters directly as req.body.data.

Is it common / good practice to use $.ajax(...) on client and node.js on server?

$.ajax(...) on client :- Of-course , why not.. don't we use $.ajax(...) when we use different backend technologies like .net or java? So there is no need for changing your front end, It is just you are using a different technology on the back-end

node on server :- Now this is quite opinion based, node is new, we javascript developers love node, because it is single technology on serverside and client as well, I would say node is fantastic.. but there are other people who would say node is crap. I would rather skip answering this part.

node.js introduced a new way of thinking, making ajax not-needed-anymore...

I think you are pointing towards websockets. yes node is a different way of thinking. This does not really replace ajax, you can find some info here

The purpose of WebSockets is to provide a low-latency, bi-directional, full-duplex and long-running connection between a browser and server. WebSockets open up new application domains to browser applications that were not really possible using HTTP and AJAX (interactive games, dynamic media streams, bridging to existing network protocols, etc).

If you don't need the specific benefits that WebSockets provide, then it's probably a better idea to stick with existing techniques like AJAX and Comet because this allows you to re-use and integrate with a huge existing ecosystem of tools, technologies, security mechanisms, knowledge bases (i.e. far more people on stackoverflow know HTTP/Ajax/Comet than WebSockets), etc.

And this is not node.js specific thing, you can still implement websockets in other technologies as well.

So node.js introduced a new way of thinking, making ajax not-needed-anymore is wrong !