简体   繁体   English

Node.js与网站交互

[英]Node.js interacting with Website

So I have this node.js app that I am running on my computer which receives items from steam users and I want the app to be able to send the item information received via some sort of method to a website to store on a db. 因此,我有一个正在运行的计算机上的node.js应用程序,该应用程序从Steam用户那里接收项目,并且我希望该应用程序能够将通过某种方法接收到的项目信息发送到网站以存储在数据库中。 The part where I store the info in a db should be easy if I can get the info to the php stage. 如果我可以将信息存储到php阶段,则将信息存储在数据库中的部分应该很容易。 And no I can't just directly send the data to a mySQL server (I know this can be done), because my website host doesn't allow remote SQL connections. 而且不行,我不能直接将数据发送到mySQL服务器(我知道可以做到),因为我的网站主机不允许远程SQL连接。 :( The problem is I don't know how to setup a communication method between the node.js app and site, preferably using php on the site end. :(问题是我不知道如何在node.js应用程序和网站之间设置通信方法,最好在网站端使用php。

The method I thought of was if I could somehow use http requests to send json data or something simple like that detailing the item information. 我想到的方法是,如果我能以某种方式使用http请求发送json数据或诸如此类的详细信息的简单信息。 And this is what I have so far to work with. 到目前为止,这是我要处理的。

On the website side: 在网站方面:

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

Its a simple way of logging what data is inputed to this page. 它是记录将什么数据输入到此页面的简单方法。

And in node.js: 在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();

I tried using this to send data to the php script but that just returned this. 我尝试使用它来将数据发送到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>

Which as I understand it, tells me that I did something wrong in the transmission stage from node.js. 据我了解,这告诉我在node.js的传输阶段发生了错误。

So thats my problem, I hope somebody can point me in the right direction. 因此,这就是我的问题,希望有人能指出正确的方向。
Also, sorry if this is not explained properly. 另外,如果对此没有正确解释,则对不起。

Thanks in advance, 提前致谢,

Durgen 杜尔根


EDIT: Ok, so I narrowed it down to something php/server side. 编辑:好的,所以我将其范围缩小到php / server端。 Because, I use this node.js script with this php script on a locally hosted server and got it to send and receive alright. 因为,我在本地托管的服务器上将此node.js脚本与此php脚本一起使用,并且可以发送和接收它。

node.js script 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 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?";
?>

Now my only question is why won't it work on the other server? 现在我唯一的问题是为什么它不能在其他服务器上工作?

I think the issues are the XRH isn't sending any data to the server. 我认为问题是XRH没有向服务器发送任何数据。 It should be something like: 应该是这样的:

xhr.send(YOUR_DATA_HERE)

(See here for more information on how to use the send method) and the PHP needs to echo some output after you save the data, or it will return the HTML document to the Javascript. (有关如何使用send方法的更多信息,请参见此处 ),并且PHP在保存数据后需要echo一些输出,否则它将HTML文档返回给Javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM