https://github.com/henriquecarv/nextjs-koa-middleware
NextJS koa middleware
https://github.com/henriquecarv/nextjs-koa-middleware
henriquecarv koa koa-router koajs nextjs nextjs-typescript typescript
Last synced: 8 months ago
JSON representation
NextJS koa middleware
- Host: GitHub
- URL: https://github.com/henriquecarv/nextjs-koa-middleware
- Owner: henriquecarv
- License: mit
- Archived: true
- Created: 2020-06-03T10:45:30.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T07:47:03.000Z (over 3 years ago)
- Last Synced: 2025-08-14T06:40:43.314Z (10 months ago)
- Topics: henriquecarv, koa, koa-router, koajs, nextjs, nextjs-typescript, typescript
- Language: TypeScript
- Size: 528 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nextjs-koa-middleware
NextJS koa middleware
[](https://www.codacy.com/manual/henriquecarv/nextjs-koa-middleware?utm_source=github.com&utm_medium=referral&utm_content=henriquecarv/nextjs-koa-middleware&utm_campaign=Badge_Grade)
[](https://circleci.com/gh/henriquecarv/nextjs-koa-middleware)
[](https://www.npmjs.com/package/nextjs-koa-middleware)
[](https://www.npmjs.com/package/nextjs-koa-middleware)
[](./LICENSE)
[](https://dependabot.com)
## System Requirements
- **[NodeJS](https://nodejs.org/en/)** (version >= 12).
## Installation
```sh
# npm
npm i nextjs-koa-middleware
# yarn
yarn add nextjs-koa-middleware
```
## Peer Dependency
- **[koa-router](https://github.com/koajs/router)** (version <= 8.0.8)
## Usage
- Define environment variables:
```sh
NODE_ENV=development
# Relative path to .next directory
NEXT_CLIENT_DIR=/
NEXT_CONFIG_PATH=/next.config.js
```
- Define NextJS Routes:
```typescript
import Router from 'koa-router';
import { handle, render, defaultReturn } from 'nextjs-koa-middleware';
const nextRouter = new Router();
nextRouter.use(defaultReturn());
nextRouter.get('/PagePath', render('pageComponentName'));
nextRouter.all('*', handleRoutes());
export default nextRouter;
```
- Start koa server:
```typescript
import koa from 'koa';
import nextRouter from './NextRouter';
const app = new Koa();
const port = process.env.PORT ?? 3000;
const host = process.env.HOST ?? 'localhost';
app.use(nextRouter.routes()).use(nextRouter.allowedMethods());
app.listen(port);
console.info(`listening on ${host}:${port}`);
```
- [Example Project](https://github.com/henriquecarv/bundle-size)
### License
Copylefted (c) 2020 [Henrique Carvalho da Cruz][1] Licensed under the [MIT license][2].
[1]: https://henriquecarv.com
[2]: ./LICENSE