Node.js与网站

时间:2018-01-31 00:48:09

标签: php html node.js request xmlhttprequest

所以我有这个我在计算机上运行的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的传输阶段我做错了。

这就是我的问题,我希望有人可以指出我正确的方向 另外,如果没有正确解释,请抱歉。

提前致谢,

Durgen

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

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?";
?>

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

1 个答案:

答案 0 :(得分:0)

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

xhr.send(YOUR_DATA_HERE)

(有关如何使用send方法的详细信息,请参阅here)以及保存数据后PHP需要echo某些输出,否则它会将HTML文档返回给Javascript。

相关问题