要解决"Apollo Server的健康检查未被调用"问题,您可以按照以下步骤进行操作:
下面是一个使用Express框架和apollo-server-express库的示例代码,演示了如何正确设置Apollo Server的健康检查路由:
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
// 创建Express应用程序
const app = express();
// Apollo Server配置
const server = new ApolloServer({
// ... 其他配置项
});
// 将Apollo Server中间件应用于指定的路由
server.applyMiddleware({ app, path: '/graphql' });
// 添加健康检查路由
app.get('/health', (req, res) => {
res.send('Apollo Server is healthy!');
});
// 启动服务器
app.listen({ port: 4000 }, () =>
console.log(`Apollo Server is running at http://localhost:4000/graphql`)
);
在上面的示例中,我们在/health
路径上添加了一个GET请求处理程序,返回一个简单的健康检查消息。您可以根据自己的需求自定义此消息。
确保在Apollo Server实例中正确配置健康检查路由,并且路由路径与您在Express应用程序中设置的路径匹配。这样,当向/health
发出GET请求时,您应该能够看到健康检查消息。
希望这可以帮助您解决问题!