简体   繁体   English

这个 javascript 前端、NodeJS 后端方案是否可行?

[英]Is this javascript frontend, NodeJS backend scenario viable to work?

I have a Domain-A which is static site built on netlify to provide shopping cart facilities.我有一个域-A,即 static 站点,该站点建立在 netlify 上,用于提供购物车设施。

I have a Domain-B server backend running a payment on Node.JS, and processing Auth Services via passport.js.我有一个 Domain-B 服务器后端在 Node.JS 上运行支付,并通过 passport.js 处理身份验证服务。

As a static site on Netlify this Domain-A cannot provide any backend services.作为 Netlify 上的 static 站点,此域-A 无法提供任何后端服务。 The user sees this domain's web pages, selects products, and eventually presses a buy button.用户看到该域的 web 页面,选择产品,最终按下购买按钮。 This button invokes JS code running in the browser, sending an AJAX /login to Domain-B.此按钮调用在浏览器中运行的 JS 代码,将 AJAX /login 发送到 Domain-B。 It successfully logs in. It returns a connect.sid cookie to Domain-A with SameSite:none, Secure:true.它成功登录。它使用 SameSite:none, Secure:true 向 Domain-A 返回一个 connect.sid cookie。 I can see the cookie with Chrome Debugger.我可以使用 Chrome 调试器查看 cookie。

After a successful login return from Domain-B, Domain-A now sends an AJAX GET to Domain-B requesting seller and payment information.在从域 B 成功登录返回后,域 A 现在向域 B 发送 AJAX GET 请求卖家和付款信息。 But the GET always fails with a 403 "User Not Logged In".但是 GET 总是以 403“用户未登录”失败。

The AJAX GET includes AJAX GET 包括

crossDomain: true,
headers: { 
    "Content-Type": "text/plain",
    "withCredentials": true
},

The Content-Type is to make it a CORS Simple Get and avoid Preflight issues, the withCredentials to make sure the cookie is included with the GET. Content-Type 使其成为 CORS 简单获取并避免 Preflight 问题,withCredentials 以确保 cookie 包含在 GET 中。 I tried using a POST but it seems to always trigger Preflight processing which creates a lot of other complexities.我尝试使用 POST,但它似乎总是触发 Preflight 处理,这会产生许多其他复杂性。

My basic question is, should I be able to do this, login and perform a GET from browser code of the Domain-A static site, to a NodeJS server on Domain-B?我的基本问题是,我是否应该能够做到这一点,从 Domain-A static 站点的浏览器代码登录并执行 GET 到 Domain-B 上的 NodeJS 服务器? OR, must I get off Netlify and put it onto something like DigitalOcean where Domain-A has a backend which can login and get or post to Domain-B?或者,我是否必须离开 Netlify 并将其放到 DigitalOcean 之类的东西上,其中 Domain-A 有一个可以登录并获取或发布到 Domain-B 的后端?

Is there sample code somewhere where I can copy the structure to my scenario?是否有示例代码可以将结构复制到我的场景中? Thx.谢谢。

try moving withCredentials outside of headers尝试将withCredentials headers之外

crossDomain: true,
headers: { 
    "Content-Type": "text/plain",
},
withCredentials: true

Also, on your backend server, do you have cors configured to allow what your frontend is trying to do?另外,在您的后端服务器上,您是否配置了 cors 以允许您的前端尝试执行的操作?

 cors({
    origin: 'netlifysite_uri',
    credentials: true,
    allowedHeaders: ['Content-Type'],
  })

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

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