[英]How does my website know to connect with my Node app?
If I have Node app running on Heroku and it creates a server and listens to say port 80. How does my website know to interact with the code in my node.js app?如果我在 Heroku 上运行 Node 应用程序,它会创建一个服务器并侦听端口 80。我的网站如何知道与我的 node.js 应用程序中的代码进行交互?
Essentially, I am trying to send an object created on my client side with Ajax to the server-side.本质上,我试图将在我的客户端使用 Ajax 创建的对象发送到服务器端。 How can my node.js app listen for this data, especially in production?
我的 node.js 应用程序如何侦听此数据,尤其是在生产中?
More info:更多信息:
So I have a static website view with html and css uploaded through shared hosting.所以我有一个静态网站视图,其中包含通过共享主机上传的 html 和 css。 I am trying to implement a simple payment system through Stripe using Node.js.
我正在尝试使用 Node.js 通过 Stripe 实现一个简单的支付系统。 So I only have one server for Node and a shared host for my website.
所以我只有一台用于 Node 的服务器和一台用于我的网站的共享主机。 My question was how can I connect the two without having to serve the .html files through Node?
我的问题是如何连接两者而不必通过 Node 提供 .html 文件?
It's unclear exactly where you got stuck in setting up your node.js server as pretty much everything you've asked for is explained in many examples on the web.目前还不清楚您在设置 node.js 服务器时究竟遇到了什么问题,因为您要求的几乎所有内容都在网络上的许多示例中进行了解释。 So, since I don't know where you got stuck, I'll explain conceptually the steps you go through to field an Ajax call from your site:
因此,由于我不知道您卡在哪里,我将从概念上解释您从您的站点发出 Ajax 调用所经历的步骤:
FYI, if the only think you were going to do with your node.js server is use Stripe checkout, then you may be able to write Javascript in your web page that communicates directly with the Strip service without evening having your own node.js server.仅供参考,如果您对 node.js 服务器的唯一想法是使用 Stripe checkout,那么您可以在网页中编写 Javascript 直接与 Strip 服务通信,而无需晚上拥有自己的 node.js 服务器. It depends upon the details of what you're trying to do.
这取决于您要尝试执行的操作的详细信息。 Some Strip checkout details here: https://stripe.com/docs/checkout
此处的一些 Strip 结帐详细信息: https : //stripe.com/docs/checkout
Because you declare your routes (assuming you're using something like Express or you've made your own vanilla http server) so when you visit a route it knows to execute whatever function you've tied this too因为您声明了您的路线(假设您使用的是 Express 之类的东西,或者您已经制作了自己的 vanilla http 服务器)所以当您访问路线时,它知道执行您也绑定的任何功能
eg app.get('/', someController);
例如
app.get('/', someController);
knows that whenever somebody visits /
it will make someController
execute.知道每当有人访问
/
它会使someController
执行。
Node directly runs the website through your program its self and knows these things Node通过自己的程序直接运行网站,知道这些
If I misunderstood let me know如果我误解了让我知道
Reply to comment:回复评论:
I'm not quite understanding your question, if you upload your view
files eg your home.ejs/html/jade
whatever you're using to another server, then upload your app
to another server, it won't work you'll get 404 not found or something.我不太明白你的问题,如果你上传你的
view
文件,例如你的home.ejs/html/jade
无论你正在使用什么到另一台服务器,然后将你的app
上传到另一台服务器,它不会工作,你会得到404未找到或什么的。 I'm assuming you came from PHP or something where you have your PHP files and everything is combined like you have your html/php
in one file and then when you visit your localhost/home.php it works.我假设你来自 PHP 或者你有你的 PHP 文件的东西,一切都组合在一起,就像你在一个文件中拥有你的
html/php
然后当你访问你的 localhost/home.php 时它工作。 Node is different you have your app
which through your server side logic you tell it what view to call, your client side stuff (like your views and css/js and what not) are completely separate from your server side logic, and through your controllers, you tell what function to call what view, eg Node 是不同的,你有你的
app
,它通过你的服务器端逻辑告诉它调用什么视图,你的客户端东西(比如你的视图和 css/js 以及什么不是)与你的服务器端逻辑完全分开,并通过你的控制器,你告诉什么函数调用什么视图,例如
(assuming you're using express) You have this as a controller: (假设您使用的是 express)您将其作为控制器:
app.get('/', function(req, res) {
res.render('home');
});
then in your app.js
logic you have:然后在你的
app.js
逻辑中你有:
// Your Route
app.use('/', homeController)
Then in your views
you probably have那么在你
views
你可能有
home.html/jade/ejs
(extension depending on whatever templating you're using) home.html/jade/ejs
(扩展名取决于您使用的任何模板)
now when somebody visits /
the route see's this and goes ok I need to call the homeController
and so it does and it's happy, then it gets the content of the function and goes oh it wants to render a view file which then express' function res.render
will map over your views
folder until it finds something that resembles whatever you've requested, in this case: home.html
so it then renders the file and sends the response back to the client,现在,当有人访问
/
路由看到这个并且运行正常时,我需要调用homeController
并且它确实调用了它并且它很高兴,然后它获取函数的内容,然后哦它想要呈现一个视图文件,然后表达'函数res.render
将映射到您的views
文件夹,直到它找到与您请求的内容类似的内容,在这种情况下: home.html
然后它呈现文件并将响应发送回客户端,
So in short Request > Route > Controller > Function Body > Does whatever you've programmed from there
简而言之,
Request > Route > Controller > Function Body > Does whatever you've programmed from there
Long story short, your client side stuff and your server logic are completely independent from each other长话短说,您的客户端内容和您的服务器逻辑彼此完全独立
As where with your PHP
or something, you have your php file就像你的
PHP
或其他东西一样,你有你的 php 文件
<?php
<html>
</html>
?> (i think thats how it goes I haven't done php in a looooonngg time)
and then you run apache or whatever and apache will magically route to the .php
file requested, compile and render it然后你运行 apache 或其他什么,apache 会神奇地路由到请求的
.php
文件,编译并渲染它
I hope I answered a little better希望我的回答好一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.