ExpressJS like API in NextJS

Jannis Milz
2 min readJan 14, 2024

Non-members of medium can read here

Have you ever wanted an ExpressJS like API structure in your NextJS app? A good structure and easy to extend.

Let me give you three examples with having an easier API with the NPM package called next-connect.

FYI: This is not the whole code, it’s just the core of the functionality I wanted to present you.

✅ Advantages

Here are quickly five advantages of using next-connect.

  • Clean and nice structure
  • Fast and scalable
  • Easy to implement
  • TypeScript support
  • Lightweight

🚏 Route

Create a POST endpoint for /user.

NextJS:

// api/user.ts
export default function handler(req: NextRequest, res: NextResponse) {
if (req.method === 'POST') {
res.status(200).send('Worked!)
}
}

Next-Connect:

// api/user.ts
const handler = createRouter<NextApiRequest, NextApiResponse>();

// Process a POST request
handler.post(middleware…

--

--

Jannis Milz

An always-wanting-to-learn-more person. Developer advocate. Obsessed with self-improvement and efficiency.