当我通过 Express 服务器使用 octokit 的 webhook 时,它们无法工作

问题描述 投票:0回答:1

我已经使用 octokit 创建了一个 Webhook 处理程序,但通过 Express 服务器使用它时,Webhook 服务器不是。 虽然 octokit 的文档提到它支持 Web 服务器,但对我来说它返回响应 202

尽管如果我创建自己的服务器而不是快速服务器,那么它就会开始使用响应代码 200 例如:

const middleware = createNodeMiddleware(app.webhooks, { path });

// This creates a Node.js server that listens for incoming HTTP requests (including webhook payloads from GitHub) on the specified port. When the server receives a request, it executes the `middleware` function that you defined earlier. Once the server is running, it logs messages to the console to indicate that it is listening.
http.createServer(middleware).listen(port, () => {
  console.log(`Server is listening for events at: ${localWebhookUrl}`);
  console.log("Press Ctrl + C to quit.");
});

当我在快递服务器中包装没有控制台时,这不起作用。

import express, { Request, Response } from "express";
import dotenv from "dotenv";
import { createNodeMiddleware } from "@octokit/webhooks";
import fs from "fs";
import { App } from "@octokit/app";
dotenv.config();

const PORT = process.env.PORT || 8000;
const expressApp = express();
const appId = process.env.APP_ID ?? "";
const webhookSecret = process.env.WEBHOOK_SECRET ?? "";
const privateKeyPath = process.env.PRIVATE_KEY_PATH ?? "";

// This reads the contents of your private key file.
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
const octokitApp = new App({
  appId,
  privateKey,
  webhooks: { secret: webhookSecret },
});
expressApp.use(express.json());
const middleware = createNodeMiddleware(octokitApp.webhooks, {
  path: "/api/webhook",
});
expressApp.use(middleware);

octokitApp.webhooks.onAny(() => {
  console.log(`first`);
});
octokitApp.webhooks.on("pull_request.reopened", () => console.log(`first`));

expressApp.get("/", (req: Request, res: Response) => {
  return res.status(200).json({
    message: "Hello world",
  });
});
expressApp.listen(PORT, () => console.log(`Server is running at port ${PORT}`));

javascript node.js express webhooks
1个回答
0
投票

我能够通过删除

app.use(express.json())

在本地解决此问题
© www.soinside.com 2019 - 2024. All rights reserved.