BUR*_*101 5 reactjs google-cloud-platform kubernetes next.js
我在 Kubernetes(谷歌云)上部署了一个应用程序。要使部署发挥作用,它需要向“/healthz”和“/”返回 200 状态响应。我在我的 Express 服务器上设置了这个作为返回响应的路由,如下所示:
app.use('/healthz', ((_req, res) => {
logGeneral.info("Health Status check called.");
res.sendStatus(200);
}));
Run Code Online (Sandbox Code Playgroud)
但我不知道如何为在 Next.js/React 上运行的前端做同样的事情。不支持 res.send 等命令。
有人知道吗?
谢谢。
小智 28
使用 API 路由是正确的答案。在 /pages/api 中创建一个名为 healthcheck.js 的新文件,其中包含以下内容:
export default function handler(req, res) {
res.status(200).json({ "status": "ok" })
}
Run Code Online (Sandbox Code Playgroud)
端点将位于 (主机名)/api/healthcheck
Koe*_*en. 14
如果您必须使用 的根路径/healthz,您可以按照其他人的建议添加 API 路由;
// ./pages/api/health.js
export default function handler(req, res) {
res.send('OK');
}
Run Code Online (Sandbox Code Playgroud)
另外,您在 中定义了重写next.config.js,因此将由;/healthz内部处理。/api/health
/** @type {import('next').NextConfig} */
module.exports = {
rewrites: async () => {
return [
{
source: '/healthz',
destination: '/api/health',
},
];
},
};
Run Code Online (Sandbox Code Playgroud)
我也有类似的需求。我必须在 AWS 上部署我的代码,我需要一个类似于 http://localhost:4000/ping 的健康检查 URL
我在页面内创建了一个名为 ping/index.js 的文件,其中包含以下内容-
// health check URL
function Ping() {
}
// This gets called on every request
export async function getServerSideProps(context) {
context.res.end('pong');
return { props: { } }
}
export default Ping;
Run Code Online (Sandbox Code Playgroud)
参考-
使用 Next.js 13 应用程序路由,您可以添加健康检查端点,如下所示:
src/app/api/health/route.ts( /api/health)
src/app/healthz/route.ts( /healthz)
export function GET() {
return NextResponse.json({});
}
Run Code Online (Sandbox Code Playgroud)
或者如果您需要纯文本
export function GET() {
return new NextResponse('OK');
}
Run Code Online (Sandbox Code Playgroud)
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
| 归档时间: |
|
| 查看次数: |
5372 次 |
| 最近记录: |