项目作者: henriquecarv

项目描述 :
NextJS koa middleware
高级语言: TypeScript
项目地址: git://github.com/henriquecarv/nextjs-koa-middleware.git
创建时间: 2020-06-03T10:45:30Z
项目社区:https://github.com/henriquecarv/nextjs-koa-middleware

开源协议:MIT License

下载


nextjs-koa-middleware

NextJS koa middleware

Codacy Badge
CircleCI
npm
npm
LICENSE
Dependabot Status

System Requirements

Installation

  1. # npm
  2. npm i nextjs-koa-middleware
  3. # yarn
  4. yarn add nextjs-koa-middleware

Peer Dependency

Usage

  • Define environment variables:
  1. NODE_ENV=development
  2. # Relative path to .next directory
  3. NEXT_CLIENT_DIR=/
  4. NEXT_CONFIG_PATH=/next.config.js
  • Define NextJS Routes:
  1. import Router from 'koa-router';
  2. import { handle, render, defaultReturn } from 'nextjs-koa-middleware';
  3. const nextRouter = new Router();
  4. nextRouter.use(defaultReturn());
  5. nextRouter.get('/PagePath', render('pageComponentName'));
  6. nextRouter.all('*', handleRoutes());
  7. export default nextRouter;
  • Start koa server:
  1. import koa from 'koa';
  2. import nextRouter from './NextRouter';
  3. const app = new Koa();
  4. const port = process.env.PORT ?? 3000;
  5. const host = process.env.HOST ?? 'localhost';
  6. app.use(nextRouter.routes()).use(nextRouter.allowedMethods());
  7. app.listen(port);
  8. console.info(`listening on ${host}:${port}`);

License

Copylefted (c) 2020 Henrique Carvalho da Cruz Licensed under the MIT license.