[英]How can I send data from a Node.js application hosted on Heroku to a PHP file hosted on a completely separate (Cpanel) server?
I can't seem to find a good answer to this question anywhere after searching for over 30 minutes.在搜索了 30 多分钟后,我似乎无法在任何地方找到这个问题的好答案。 I hope this hasn't been asked already.
我希望这还没有被问到。
So, I want to build an online game.所以,我想建立一个在线游戏。 It will be a very simple online game (mostly for practice).
这将是一个非常简单的在线游戏(主要用于练习)。 I want to host this game on Heroku, and I will code the game using Node.js.
我想在 Heroku 上托管这个游戏,我将使用 Node.js 对游戏进行编码。 Now, I have a separate server (a shared server running Apache and Cpanel) where I will want to store all the permanent data for the game (in a MySQL database).
现在,我有一个单独的服务器(一个运行 Apache 和 Cpanel 的共享服务器),我想在其中存储游戏的所有永久数据(在 MySQL 数据库中)。 So what I need to do is serve up the game using PHP on my Apache web server, allow users to create accounts/sign into accounts on the Apache server (using a MySQL database also located there), then I want them to be able to "join a room" and when they do that, I want to send them off to the Node.js server running on Heroku, and allow that server to handle everything related to gameplay (through WebSockets).
So what I need to do is serve up the game using PHP on my Apache web server, allow users to create accounts/sign into accounts on the Apache server (using a MySQL database also located there), then I want them to be able to “加入房间”,当他们这样做时,我想将它们发送到在 Heroku 上运行的 Node.js 服务器,并允许该服务器处理与游戏相关的所有事情(通过 WebSockets)。 Now, after they're finished with their game, I want to save their scores and some other related data about them into my MySQL database over on my Apache server... Is there a way I can do this?
现在,在他们完成游戏后,我想将他们的分数和其他一些关于他们的相关数据保存到我的 MySQL 数据库中,在我的 Apache 服务器上......有什么办法可以做到这一点吗? Is there any means by which I can send data from a Node.js server running on Heroku to a PHP file on my Apache server?
有什么方法可以将数据从在 Heroku 上运行的 Node.js 服务器发送到 PHP 服务器上的 PHP 文件? (For example, handlepostgame.php or something similar)
(例如,handlepostgame.php 或类似的东西)
I hope this is a clearly written question and that you're able to easily understand what I'm asking.我希望这是一个写得很清楚的问题,并且您能够轻松理解我在问什么。 If not, let me know and I'll try to make it more clear.
如果没有,请告诉我,我会尽量让它更清楚。 Thanks in advance.
提前致谢。
Thank you to @ADyson for pointing me in the right direction!感谢@ADyson 为我指明了正确的方向!
So, I found the answer that suits my needs.所以,我找到了适合我需要的答案。 This is my "requesttophp:js" file: (Node.js)
这是我的“requesttophp:js”文件:(Node.js)
var http = require('http');
console.log("running");
var query = "something=cookie";
var queryLength = query.length;
var options = {
host: 'www.mydomain.com',
path: '/path/to/fromnodejs.php',
method: 'POST',
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': queryLength
}
};
callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
}
var req = http.request(options, callback);
req.write(query);
req.end();
respondtonodejs.php: (PHP)响应nodejs.php:(PHP)
<?php
echo "it worked\n";
echo $_POST["something"];
?>
The result is结果是
running
it worked
cookie
It does exactly what I want: .) Thank you.它正是我想要的:。)谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.