[英]Node.js interacting with Website
因此,我有一个正在运行的计算机上的node.js应用程序,该应用程序从Steam用户那里接收项目,并且我希望该应用程序能够将通过某种方法接收到的项目信息发送到网站以存储在数据库中。 如果我可以将信息存储到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?";
?>
现在我唯一的问题是为什么它不能在其他服务器上工作?
我认为问题是XRH没有向服务器发送任何数据。 应该是这样的:
xhr.send(YOUR_DATA_HERE)
(有关如何使用send方法的更多信息,请参见此处 ),并且PHP在保存数据后需要echo
一些输出,否则它将HTML文档返回给Javascript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.