Questions tagged [koa-router]

Router middleware for koa.

Router middleware for koa.

https://github.com/alexmingoia/koa-router

139 questions
48
votes
2 answers

Koa router: How to get query string params?

I'm using koa-router. How can I get the request's query string params? This is the best I managed to write: import koaRouter from 'koa-router'; const router = koaRouter({ prefix: '/courses' }); router.get('/', async (ctx) => { …
Alon
  • 10,381
  • 23
  • 88
  • 152
39
votes
8 answers

How can I split my koa routes into separate files?

I'm trying to figure out how to split my routes into separate files. I have this so far, but it doesn't work. I just get Not found when I try to access http://localhost:3001/api/things //server.js var koa = require('koa'); var app = koa(); var…
chovy
  • 72,281
  • 52
  • 227
  • 295
18
votes
2 answers

Why do we await next when using koa routers?

Why do we do this router.get('/data', async (ctx, next) => { ctx.body = dummyjson.parse(data); await next(); }); router.get('/data/:x', async (ctx, next) => { const newData = dataRepeat.replace('%(x)', ctx.params.x); ctx.body =…
relidon
  • 2,142
  • 4
  • 21
  • 37
14
votes
1 answer

How can i get a list of Koa server url routes

I'm developing a mock server using koajs, and i would like to publish a service which lists developed APIs. I use koa-router for mouting services. And i would like somethink like: var business_router =…
14
votes
2 answers

How to handle a 404 in Koa 2?

I have a 404.jade file that I want to render whenever there is an invalid GET request. Here is my current code: app.js import Koa from 'koa' import views from 'koa-views' import serve from 'koa-static' import rootRoutes from './routes/index' import…
Saad
  • 49,729
  • 21
  • 73
  • 112
11
votes
2 answers

Why can't I serve static files from a Koa router?

Why in the following case does koa-static fail to work with koa-router? const Koa = require("koa") const serve = require("koa-static") const Router = require("koa-router") const app = new Koa() const router = new Router() // fails with 404...…
ChaseMoskal
  • 7,151
  • 5
  • 37
  • 50
9
votes
2 answers

GET request parameters with koa-router

http://localhost:3000/endpoint?id=83 results in 404 (Not Found). All other routes work as expected. Am I missing something here? router .get('/', function *(next) { yield this.render('index.ejs', { title: 'title set on the server' …
Out of Orbit
  • 543
  • 2
  • 5
  • 17
7
votes
3 answers

Trigger a download in Koa request handler

I'm trying to trigger a download from a POST request handler in Koa with koa-router. Essentially, I'm trying to do something like this: app.js const Koa = require('koa') const router = require('./router') const app = new…
Saad
  • 49,729
  • 21
  • 73
  • 112
7
votes
2 answers

REST API with koa2. Common prefix for several routers

I have two entities, users and employees. So I want CRUD for both in different endpoints, but both of them will be mounted under "api", so I can define api_v1, api_v2 and so on. The endpoints would be something like: get api/users put…
user2670996
  • 2,654
  • 6
  • 29
  • 45
6
votes
1 answer

Node 8 + Typescript + Koa + koa-Router throws "TypeError: ctx.onerror is not a function"

I have a very simple server to play around with: import * as http from 'http'; import * as Koa from "koa"; import { Request, Response, Context } from "koa"; import * as Router from "koa-router"; import * as bodyParser from "koa-bodyparser"; const…
wzr1337
  • 3,609
  • 5
  • 30
  • 53
5
votes
2 answers

Why can't koa-router be put before koa-cors?

I use Koa with Node.js 8.1. Today I found that in my app.js, if I write in this order: const Koa = require('koa') var cors = require('koa-cors') const app = new Koa() app.use(cors(options)) app.use(router.routes()) the cors can work. I can verify…
guo
  • 9,674
  • 9
  • 41
  • 79
5
votes
1 answer

How to Generate API docs for KOA Based server

I am having a KOA based server. now i want to generate API docs for the server. Is there any tools for auto generating API Docs
Amit Mourya
  • 538
  • 1
  • 7
  • 18
4
votes
2 answers

Combining Typescript Koa-Router and Passport

I am newb in Typescript and trying to integrate koa-router and koa-passport. Installed all @types\ import Koa from "koa"; import Route from "koa-router"; import passport from "koa-passport"; import session from "koa-session"; const app = new…
Talgat Saribayev
  • 1,898
  • 11
  • 19
4
votes
4 answers

How to serve the same static files regardless of the route?

I have a react App. With webpack I build for production. Now I try to set a little koa server to serve the static files generated by webpack for production. So I did this import Koa from 'koa' import serve from 'koa-static' const app = new…
doums
  • 470
  • 10
  • 24
4
votes
3 answers

How do I test custom Koa middleware for error handling?

As part of a migration of an older app from ExpressJs to Koa JS (v1). I've written a piece of middleware to handle any errors that occur. It looks something like this: module.errors = function * (next) { try { yield next; } catch (err) { …
purpletonic
  • 1,858
  • 2
  • 18
  • 29
1
2 3
9 10