简体   繁体   English

如何在 Keystone 6 中访问底层 Express 应用程序?

[英]How to access underlying express app in Keystone 6?

I'm using Keystone 6 for my backend and I'm trying to integrate Stripe, which requires access to the underlying express app to pass a client secret to the client from the server.我的后端使用 Keystone 6,我正在尝试集成 Stripe,这需要访问底层 express 应用程序才能将客户端机密从服务器传递给客户端。 This is highlighted in the Stripe documentation here: https://stripe.com/docs/payments/payment-intents#passing-to-client这在此处的 Stripe 文档中突出显示: https : //stripe.com/docs/payments/payment-intents#passing-to-client

I'm having trouble figuring out how to access the express app in Keystone 6 though and they don't seem to mention anything about this in the documentation.不过,我无法弄清楚如何在 Keystone 6 中访问 express 应用程序,而且他们似乎没有在文档中提及任何相关内容。 Any help would be appreciated.任何帮助,将不胜感激。

The short answer is Keystone 6 doesn't support this yet.简短的回答是 Keystone 6 尚不支持此功能。

The longer answer has two parts:较长的答案有两个部分:

This functionality is coming这个功能来了

We've been discussing this requirement internally and the priority of it has been raised.我们一直在内部讨论这个要求,并提高了它的优先级。 We're updating the public roadmap to reflect this next week.我们将在下周更新公共路线图以反映这一点。 The functionality itself should arrive soon after.功能本身应该很快就会出现。 (Unfortunately I can't commit to a release date.) (不幸的是,我无法承诺发布日期。)

Getting access to the Express app is possible , it's just a real pain right now访问 Express 应用程序是可能的,现在真的很痛苦

If you look at Keystone's start command you can see where it calls createExpressServer() .如果您查看 Keystone 的start命令,您可以看到它在何处调用createExpressServer() This just returns an express app with the GraphQL API and a few other bits and bobs.这只是返回一个带有 GraphQL API 和其他一些小东西的 express 应用程序。 But there's actually nothing forcing you to use the build in keystone start command – You can copy this code, hack it up and just run it directly yourself.但实际上并没有强迫您使用内置keystone start命令——您可以复制此代码,将其修改并直接自己运行。

Eg.例如。 you could replace this...你可以换这个...

const server = await createExpressServer(
  config,
  graphQLSchema,
  keystone.createContext,
  false,
  getAdminPath(cwd)
);

With...和...

const server = express();
server.get('/hello-world', (req, res) => {
  res.send('Hello');
});

const keystoneServer = await createExpressServer(
  config,
  graphQLSchema,
  keystone.createContext,
  false,
  getAdminPath(cwd)
);
server.use(keystoneServer);

And your /hello-world endpoint should take precedence over the stuff Keystone adds.并且您的/hello-world端点应该优先于 Keystone 添加的内容。

Unfortunately, this doesn't work for the dev command so, in your local environment you'll need to do it differently.不幸的是,这对dev命令不起作用,因此,在您的本地环境中,您需要以不同的方式执行此操作。

One option is to start a second express server that you control and put it on a different port and include your custom routes there.一种选择是启动您控制的第二个快速服务器并将其放在不同的端口上,并在那里包含您的自定义路由。 You can still do this from within your Keystone app codebase but having different URLs in different environments can be annoying.您仍然可以在 Keystone 应用程序代码库中执行此操作,但在不同环境中使用不同的 URL 可能会很烦人。 You'll probably need an environment variable just for your custom endpoints URL, with values like this in production:您可能需要一个仅用于自定义端点 URL 的环境变量,在生产中具有如下值:

# Production
GRAPHQL_ENDPOINT="https://api.example.com/api/graphql"
CUSTOM_ENDPOINT="https://api.example.com/hello-world"

And this in dev:这在开发中:

# Dev
GRAPHQL_ENDPOINT="http://localhost:3000/api/graphql"
CUSTOM_ENDPOINT="http://localhost:3100/hello-world"

It's ugly but it does work.这很丑陋,但确实有效。

I'll update this answer when the "official" functionality lands.当“官方”功能登陆时,我会更新这个答案。

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

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