Next.js API routes with Koa.js #36941
Unanswered
ivandotv
asked this question in
Show and tell
Replies: 1 comment
-
I am using this code from chatgpt: // pages/api/third-party/[...all].ts
// nextjs@13, [email protected]
import type { NextApiRequest, NextApiResponse } from "next";
import { createServer } from "http";
import Koa, { Context } from "koa";
const koaApp = new Koa();
koaApp.use(async (ctx: Context) => {
ctx.body = "Hello from Koa.js in Next.js API route!";
});
const server = createServer(koaApp.callback());
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
server.emit("request", req, res);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I've been doing some CRUD with Next.js API routes, and I've always liked
using Koa.js for creating the API endpoints in Node.js
I think Koa.js is a great fit for Next.js because it is fully async, error handling is easy, and the codebase is very small so the initialization of the Koa App is very fast.
I've decided to integrate Koa.js (and Koa Router) with the Next.js API routes:
Usage is simple as this (in your API file):
Check out the repository, where you can find complete documentation and a small Next.js demo app.
I'm open to suggestions and contributions.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions