简体   繁体   English

我的网站如何知道连接到我的 Node 应用程序?

[英]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 调用所经历的步骤:

  1. Set up your node.js server listening on a unique port on a host that is in DNS.设置您的 node.js 服务器侦听 DNS 中主机上的唯一端口。 Since this is a different server than your website, it will need to either be a different host or a different port (if on the same host).由于这是与您的网站不同的服务器,因此它需要是不同的主机或不同的端口(如果在同一主机上)。
  2. Enable CORS in your node.js app so that your node.js server will let the browser do cross origin requests to it.在您的 node.js 应用程序中启用 CORS,以便您的 node.js 服务器允许浏览器向它发出跨源请求。 There is an NPM module that can make this pretty easy for you.有一个 NPM 模块可以让你很容易地做到这一点。
  3. Set up the shell of your node.js web server.设置 node.js Web 服务器的外壳。 I would personally use Express to make it simpler.我个人会使用 Express 使其更简单。
  4. Create a request handler in your server for a "post" on a particular route.在您的服务器中为特定路由上的“发布”创建一个请求处理程序。 This will allow your web server to listen for a specific request.这将允许您的 Web 服务器侦听特定请求。
  5. Write the code in that request handler that parses the request and does whatever it is you want to do with the request.在该请求处理程序中编写代码来解析请求并执行您想要对请求执行的任何操作。

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.

相关问题 如何将我的网站连接到我的节点应用程序? - How to connect my website to my node app? 我已经通过云9创建了一个节点应用程序,但是如何将应用程序与网站连接起来? - I've created a node app through cloud 9, but how do I connect my app with my website? 如何使用Nginx将我的域连接到我的节点应用程序 - How to connect my domain to my node app with nginx 将node.js脚本与我的网站连接 - Connect node.js script with my website 如何将离子应用程序连接到cpanel节点服务器? - How can I connect my ionic app to cpanel node server? 如何知道我的套接字连接成功与否? - How to know my socket connect is success or not? 如何将在 android 设备中运行的 Ionic 应用程序连接到我的电脑(本地主机)中的节点服务器? - How can I connect my Ionic app running in my android device to my node server in my pc (localhost)? 如何将 node.js 文件连接到我的真实网站(不是本地主机)? - How can I connect node.js file to my real website (not localhost)? 如何连接我的移动应用程序(用 lua 编写)和我的服务器(用 node.js 编写)? - How to connect my mobile app (written in lua) and my server (written in node.js)? 如何将我的本地 node.js 应用程序连接到我的 Heroku Postgres 数据库? - How can I connect my local node.js app to my Heroku Postgres database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM