Questions tagged [koa]

Koa is a web framework which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.

Koa is a web framework for Node.js, designed by the team behind Express, aiming to be a smaller, more expressive, and more robust foundation for web applications and APIs.

Use this tag if your question is about the application middleware generators and its cascading properties or the use of App.Settings. Note also the existence of the tag if you're using the middleware API with async/await.

Resources

1232 questions
12
votes
5 answers

Access the raw body of a request in koa.js

I have created an API using io.js and koa.js. As a body parser middleware I am using koa-body, which in turn uses co-body. On one of my API endpoints I am receiving POST requests, and I need access to the raw body of the request, because I need to…
Ivan Stoyanov
  • 5,412
  • 12
  • 55
  • 71
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
11
votes
3 answers

strapi - restrict user to fetch only data related to him

Usually, a logged-in user gets all entries of a Content Type. I created a "snippets" content type (_id,name,content,users<<->>snippets) <<->> means "has and belongs to many" relation. I created some test users and make a request: curl -H…
sgohl
  • 391
  • 3
  • 17
11
votes
3 answers

Get client IP in Koa.js

I have a Koa app with a handler like this: router.get('/admin.html', function *(next) { const clientIP = "?"; this.body = `Hello World ${clientIp}`; }); where I need to acquire the client's IP address to form the response. How can I assign…
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
11
votes
1 answer

Why need more than one secret key on koa?

Sorry, I don't quite understand how secret key works in koa. In koa, there is a keys field on app object which will be used like this: const app = new Koa(); app.keys = ['some secret', 'another secret', 'or more ...']; // it's an …
Mas Bagol
  • 4,377
  • 10
  • 44
  • 72
11
votes
4 answers

Koa.js - serving static files and REST API

I'm new to koa.js library and I need some help. I'm trying to make simple REST application using koa. I have a static html and javascript files I want to serve on route / and REST API accessing from /api/. This is my project directory…
suricactus
  • 1,210
  • 2
  • 15
  • 22
11
votes
2 answers

What's the best way to pass values among middlewares in koa.js

I have a simple setup for koa.js with koa-route and koa-ejs. var koa = require('koa'); var route = require('koa-route'); var add_ejs = require('koa-ejs'); var app = koa(); add_ejs(app, {…}); app.use(function *(next){ console.log( 'to…
beatak
  • 9,185
  • 10
  • 33
  • 42
10
votes
2 answers

MongoDB queries are taking 2-3 seconds from Node.js app on Heroku

I am having major performance problems with MongoDB. Simple find() queries are sometimes taking 2,000-3,000 ms to complete in a database with less than 100 documents. I am seeing this both with a MongoDB Atlas M10 instance and with a cluster that I…
Corey Quillen
  • 1,566
  • 4
  • 24
  • 52
10
votes
2 answers

Troubles with koa-cors and Access-Control-Allow-Credentials

I have this error XMLHttpRequest cannot load http://127.0.0.1:1337/. Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the…
8txra
  • 147
  • 1
  • 2
  • 9
10
votes
1 answer

How to redirect some URL conditionally in the koa 2

This is what i'm thinking, pseudo code. const myRedirect = (routePath) => { newUrl = routePath; if (matches condition) newUrl = do_some_modification(routePath); return next(newUrl); } const myFunc = (routePath,…
user3552178
  • 2,719
  • 8
  • 40
  • 67
10
votes
2 answers

Koa's `ctx.status` not getting sent to client

Here is my simple route: router.post('/getFile', async (ctx) => { const fileName = `${ctx.request.body.file}.pdf`; const file = fs.createReadStream(fileName); // This file might not exist. file.on('error', (err) => { ctx.response.status =…
Saad
  • 49,729
  • 21
  • 73
  • 112
10
votes
2 answers

Handling JWT expiration and JWT payload update

I have a Koa based Node.js backend for my personal/hobby application. I implemented session handling with JWT tokens. The client (AngularJS) gets the token after a successful login and stores the token somewhere (currently in sessionStorage but for…
Attila Kling
  • 1,717
  • 4
  • 18
  • 32
10
votes
2 answers

Whats the point of composing middleware in Koa?

I am diving into Koa2 and I see koa-compose. I get that I give it middlewares and it returns one, but why? What is the benefit of having multiple middleware wrapped as one instead of just adding them separately? app.use(compose(m1,…
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
10
votes
1 answer

webpack-dev-server with backend api

I am using webpack-dev-server for an angularjs app, I start it from a task in package.json like this: "scripts": { "start-api": "node api/server.js", "dev": "webpack-dev-server --env dev --history-api-fallback --inline --progress --port…
dagda1
  • 26,856
  • 59
  • 237
  • 450
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
1 2
3
82 83