【问题标题】:Node.js interacting with WebsiteNode.js 与网站交互
【发布时间】:2018-07-10 00:15:03
【问题描述】:

所以我在我的计算机上运行了这个 node.js 应用程序,它接收来自 Steam 用户的项目,我希望该应用程序能够将通过某种方法接收到的项目信息发送到网站以存储在D b。如果我可以将信息获取到 php 阶段,我将信息存储在数据库中的部分应该很容易。不,我不能直接将数据发送到 mySQL 服务器(我知道可以这样做),因为我的网站主机不允许远程 SQL 连接。 :(问题是我不知道如何设置node.js应用程序和站点之间的通信方法,最好在站点端使用php。

我想到的方法是,如果我能以某种方式使用 http 请求发送 json 数据或类似详细说明项目信息的简单方法。这就是我目前要做的工作。

在网站方面:

<?php
/* register.php */

header("Content-type: text/plain");
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Data\n";
fwrite($myfile, $txt);
$txt = $_POST . $_GET . "\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

这是一种记录输入到此页面的数据的简单方法。

在 node.js 中:

/* sendrequest.js */
var sys = require('util');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    sys.puts("State: " + this.readyState);

    if (this.readyState === 4) {
        sys.puts("Complete.\nBody length: " + this.responseText.length);
        sys.puts("Body:\n" + this.responseText);
    }
};

xhr.open("GET", "www.my-site.com/register.php");
xhr.send();

我尝试使用它向 php 脚本发送数据,但它只是返回了这个。

State: 1
State: 1
(node:7328) DeprecationWarning: util.puts is deprecated. Use console.log instead
.
State: 2
State: 3
State: 4
Complete.
Body length: 851
Body:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>funct
ion toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))})
;return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].cons
tructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":""
)+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c
63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("e701
ec1290eafb233a5b83622b74d5f1");document.cookie="__test="+toHex(slowAES.decrypt(c
,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://
name.byethost10.com/trade/db_interact.php?i=1";</script><noscript>This site requ
ires Javascript to work, please enable Javascript in your browser or use a brows
er with Javascript support</noscript></body></html>

据我了解,告诉我我在 node.js 的传输阶段做错了。

这就是我的问题,我希望有人能指出我正确的方向。
另外,如果解释不正确,请见谅。

提前致谢,

杜根


编辑: 好的,所以我将其缩小到 php/server 端。因为,我在本地托管的服务器上使用这个 node.js 脚本和这个 php 脚本并让它发送和接收正常。

node.js 脚本

var request = require('request');

// Set the headers
var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
}

// Configure the request
var options = {
    url: 'http://127.0.0.1:8080/db_interact.php',
    method: 'POST',
    headers: headers,
    form: {'mes': 'testing'}
}

// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log(body)
    }
})

db_interact.php

<?php
header("Content-type: application/x-www-form-urlencoded");

$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Data\r\n";
fwrite($myfile, $txt);
$txt = $_POST['mes'] . $_POST . "\n";
fwrite($myfile, $txt);
fclose($myfile);
echo "done\n";
$txt = $_POST['mes'];
echo $txt . "\n";
echo "worked?";
?>

现在我唯一的问题是为什么它不能在其他服务器上运行?

【问题讨论】:

  • 实际上有数千种方法可以将数据从一台服务器发送到另一台服务器。一种经典的方法是使用 http 并发送带有数据的 http post,然后在另一端你有一个带有该帖子的请求处理程序的 http 服务器,它可以从帖子中获取数据并将其放入 sql 数据库中。由于这可能是在开放的互联网上,因此您可能需要在帖子中提供一些凭据。至少有 1000 种其他方法可以做到这一点。您可以使用 node.js 的 request 库将 http 帖子发送到任何其他服务器。
  • 这几乎正是我想要的,但我在网上找不到你所说的信息。因此,一些直接链接或使用示例会很有帮助。
  • 我现在正在使用手机,但您可以搜索“npm request”,然后会找到请求库。
  • 谢谢。我已经看过了,但我会再看一遍。再次感谢。
  • 表示request成功。检查您的php 项目,是否有任何拦截器来响应您不想要的结果,或者类似http 301 redirect

标签: php html node.js request xmlhttprequest


【解决方案1】:

我认为问题在于 XRH 没有向服务器发送任何数据。它应该是这样的:

xhr.send(YOUR_DATA_HERE)

(有关如何使用 send 方法的更多信息,请参阅here)并且 PHP 需要在您保存数据后向echo 一些输出,否则它将 HTML 文档返回给 Javascript。

【讨论】:

  • 谢谢,看起来您链接到的页面上的信息和示例将为我提供更多答案。这是在此类事物之间发送数据的标准/正常方式吗?
  • 是的,这很正常。您也可以尝试在 JSON format 中从 Javascript 发送数据,然后在 PHP 中使用 decode 发送数据。
猜你喜欢
  • 1970-01-01
  • 2012-11-08
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2011-11-08
相关资源
最近更新 更多