gpt4 book ai didi

cloud-foundry - 如何从带有目的地的 html5 应用程序轻松使用 nodejs-backend?

转载 作者:行者123 更新时间:2023-12-04 05:15:59 24 4
gpt4 key购买 nike

我想将 html5 模块与 nodejs 后端“连接”起来,以便前端可以访问后端 以获取数据库逻辑。我发现,我必须在 scp 中创建一个目的地并将其写入 mta.yaml 和 xs-app.js 文件中以供批准者使用。不幸的是,它无法正常工作,因为存在错误:“未找到”。

html5 前端只发出一个 ajax 请求。nodjs 后端通过 express 接收请求并使用 db 进行操作。

我在 scp 中创建了一个名为 backendApi 的目的地。 url 是 node_backend 之一。

mta.yaml 文件中的代码片段:

name: node_backend
type: nodejs
path: node_backend
requires:
- name: mongodb-nemo-t01-service
- name: cf_elb_postgres
provides:
- name: node_backend_api
properties:
url: '${default-url}'

- name: cfElbTimeline
type: html5
path: cfElbTimeline
parameters:
disk-quota: 500M
memory: 500M
build-parameters:
builder: grunt
requires:
- name: node_backend_api
group: destinations
properties:
name: backendApi
url: '~{url}'
forwardAuthToken: true

我的 xs-app.js 文件:

{
"welcomeFile": "/index.html",
"authenticationMethod": "route",
"logout": {
"logoutEndpoint": "/do/logout"
},
"routes": [{
"source": "^(.*)$",
"target": "$1",
"service": "html5-apps-repo-rt",
"authenticationType": "xsuaa"
}, {
"source": "^(.*)$",
"target": "$1",
"destination": "backendApi",
"httpMethods": ["GET", "POST"],
"authenticationType": "none"
}]
}

通过前端访问后端已经成功了一次,但是html5 应用程序存储库 出现问题,因此 View 不可见。所以我改变了它,但不能回到那个点,当我可以通过 approuter url 访问后端时。也许路由的正则表达式有问题?

任何人都可以检查我的代码或可以解释它应该如何工作吗?

最佳答案

在匹配相同模式时,xs-app.json 中定义的路由按条目顺序考虑。意思是,您认为可能对 API 发出的任何请求都由第一条路线提供服务:即;仅包含静态文件的 HTML5 repo 服务。

此外,最好区分路线以避免混淆。您可以通过添加路由前缀或完全不同的模式来区分 API 路由。

例如:

{
"welcomeFile": "/index.html",
"authenticationMethod": "route",
"logout": {
"logoutEndpoint": "/do/logout"
},
"routes": [{
"source": "^(.*)$",
"target": "$1",
"service": "html5-apps-repo-rt",
"authenticationType": "xsuaa"
}, {
"source": "^/api/(.*)$",
"target": "$1",
"destination": "backendApi",
"httpMethods": ["GET", "POST"],
"authenticationType": "none"
}]
}

然后您就可以像这样从批准者访问目的地:

https://<approuter_url>/<app_name-version>/api/whatever.xsodata

关于cloud-foundry - 如何从带有目的地的 html5 应用程序轻松使用 nodejs-backend?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55353211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com