How to use @koa/router - 10 common examples

To help you get started, we’ve selected a few @koa/router examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xiadd / zhuishushenqi / src / router / index.js View on Github external
import Router from '@koa/router'
import categoryController from '../controller/category'
import bookController from '../controller/book'
import rankController from '../controller/rank'
import commentsController from '../controller/comment'
import bookListController from '../controller/booklist'
import searchController  from '../controller/search'
import SystemController from '../controller/system'
import homeController from '../controller/home'

const router = new Router()

//设置前缀
// router.prefix('/api')
/**
 * 分类相关路由
 */

// 首页
router.get('/', homeController.index)

// 登录
// router.get('/authenticate', UserController.userLogin)
//router.get('/authenticate', UserController.userLogin)

//设置jwt 权限
// router.use(jwt({ secret: JWTSecret }))  如果需要授权的话
github uploadcare / uploadcare-upload-client / mock-server / server.ts View on Github external
import addTrailingSlashes from 'koa-add-trailing-slashes'
// @ts-ignore
import koaBody from 'koa-body'

import logger from './middleware/logger'
import delayer from './middleware/delayer'
import auth from './middleware/auth'

// Config
import { PORT } from './config'

// Routes
import { ROUTES, RouteType } from './routes'

const app = new Koa()
const router = new Router()

const silent = process.argv.includes('--silent')
const noop = (_, next) => next()

// Use middleware
app.use(cors())
app.use(addTrailingSlashes())
app.use(silent ? noop : logger)
app.use(delayer)
app.use(
  koaBody({
    multipart: true,
    formLimit: 50 * 1024 * 1024,
    textLimit: 50 * 1024 * 1024
  })
)
github velopert / velog-server / src / routes / api / v2 / auth / index.ts View on Github external
import Router from '@koa/router';
import Joi from 'joi';
import social from './social';
import { getRepository } from 'typeorm';
import { validateBody } from '../../../../lib/utils';
import User from '../../../../entity/User';
import EmailAuth from '../../../../entity/EmailAuth';
import shortid = require('shortid');
import { createAuthEmail } from '../../../../etc/emailTemplates';
import sendMail from '../../../../lib/sendMail';
import { generateToken, decodeToken, setTokenCookie } from '../../../../lib/token';
import { decode } from 'punycode';
import UserProfile from '../../../../entity/UserProfile';
import VelogConfig from '../../../../entity/VelogConfig';

const auth = new Router();

/* LOCAL AUTH */

/**
 * Send Auth Email
 *
 * POST /api/v2/auth/sendmail
 * {
 *  email: "public.velopert@gmail.com"
 * }
 */
auth.post('/sendmail', async ctx => {
  type RequestBody = {
    email: string;
  };
  const schema = Joi.object().keys({
github jaylineko / discohook / src / core / server.tsx View on Github external
import React from "react"
import { renderToNodeStream } from "react-dom/server"
import { Transform } from "stream"
import { createDeflate, createGzip } from "zlib"
import App from "./App"

Object.assign(global, {
  ENV: process.env.NODE_ENV ?? "production",
  PROD: process.env.NODE_ENV === "production",
  DEV: process.env.NODE_ENV === "development",
  TEST: process.env.NODE_ENV === "test",
  SERVER: true,
})

const app = new Koa()
const router = new Router()

const build = resolve(__dirname, "../../dist")
const html = readFileSync(resolve(build, "index.html")).toString()
const [templateStart, templateEnd] = html
  .replace('<div id="app"></div>', '<div id="app">{app}</div>')
  .split("{app}")
const [templateStartBots, templateEndBots] = html
  .replace(/
github velopert / velog-server / src / routes / api / v2 / files / index.ts View on Github external
import Koa from 'koa';
import Router from '@koa/router';
import Joi from 'joi';
import { validateBody } from '../../../../lib/utils';
import AWS from 'aws-sdk';
import mime from 'mime-types';
import authorized from '../../../../lib/middlewares/authorized';
import UserImage from '../../../../entity/UserImage';
import { getRepository } from 'typeorm';
import createLoaders from '../../../../lib/createLoader';

const BUCKET_NAME = 's3.images.velog.io';

const files = new Router();

const s3 = new AWS.S3({
  region: 'ap-northeast-2',
  signatureVersion: 'v4'
});

const generateSignedUrl = (path: string, filename: string) => {
  const contentType = mime.lookup(filename);
  if (!contentType) {
    const error = new Error('Failed to parse Content-Type from filename');
    error.name = 'ContentTypeError';
    throw error;
  }
  if (!contentType.includes('image')) {
    const error = new Error('Given file is not a image');
    error.name = 'ContentTypeError';
github eankeen / tails / operator / routes / index.js View on Github external
import Router from '@koa/router'

import { errorController } from '../controllers/errorController'
import { rootController } from '../controllers/rootController'
import apiRouter from './apiRouter'

const router = new Router()

router.use(errorController)
router.get('/', rootController)
router.use('/api', apiRouter)

export default router.routes()
github velopert / velog-server / src / routes / api / v2 / index.ts View on Github external
import Router from '@koa/router';
import auth from './auth';
import { consumeUser } from '../../../lib/token';
import files from './files';

const v2 = new Router();

v2.get('/check', ctx => {
  ctx.body = {
    version: 'v2'
  };
});

v2.get('/test', async ctx => {
  ctx.body = {
    user_id: ctx.state.user_id
  };
});

v2.use('/auth', auth.routes());
v2.use('/files', files.routes());
github velopert / velog-server / src / routes / api / v2 / auth / social.ts View on Github external
import Router from '@koa/router';
import { callbackify } from 'util';

const social = new Router();

const { GITHUB_ID, GITHUB_SECRET } = process.env;

if (!GITHUB_ID || !GITHUB_SECRET) {
  throw new Error('GITHUB_ID, GITHUB_SECRET ENVVAR IS MISSING');
}
type SocialProvider = 'facebook' | 'github' | 'google';

function generateSocialLoginLink(provider: SocialProvider) {
  const generators = {
    github() {
      return `https://github.com/login/oauth/authorize?scope=user:email&client_id=${GITHUB_ID}`;
    },
    facebook() {
      return '';
    },
github velopert / velog-server / src / routes / api / index.ts View on Github external
import Router from '@koa/router';
import v2 from './v2';

const api = new Router();
api.use('/v2', v2.routes());

export default api;
github velopert / velog-server / src / routes / index.ts View on Github external
import Router from '@koa/router';
import api from './api/index';

const routes = new Router();

routes.use('/api', api.routes());
routes.get('/', ctx => {
  ctx.body = 'hello world!';
});

export default routes;

@koa/router

Router middleware for koa. Maintained by Forward Email and Lad.

MIT
Latest version published 2 months ago

Package Health Score

92 / 100
Full package analysis

Popular @koa/router functions