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
61
votes
1 answer

Koa / Co / Bluebird or Q / Generators / Promises / Thunks interplay? (Node.js)

I'm investigating building a web app in part with Koa, but I don't quite have a handle on the hows, whens, and whys of choosing between - and applying - the range of supportive "making async easier" technologies/approaches (listed below). Overall…
JLS
  • 691
  • 1
  • 6
  • 10
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
31
votes
1 answer

How do I set headers to all responses in Koa.js?

In Express.js I used to have this kind of code: app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); …
Gherman
  • 6,768
  • 10
  • 48
  • 75
30
votes
1 answer

Typescript extend third-party declaration files

How can I extend third-party declaration files? for example, I want to extend Context from @types/koa and add an extra field(resource) to it. I tried this: // global.d.ts declare namespace koa { interface Context { resource: any; …
Saman Mohamadi
  • 4,454
  • 4
  • 38
  • 58
24
votes
4 answers

How to parse multipart/form-data body with Koa?

Because I spent some (too much) time figuring out this simple requirement. I am documenting here the way to achieve multipart/form-data body parsing with Koa. In my case, the reason of the confusion was the number of alternatives available out…
eightyfive
  • 4,601
  • 3
  • 35
  • 44
21
votes
1 answer

How to use socket.io together with webpack-hot-middleware?

I have a Koa server using webpack-dev-middleware and webpack-hot-middleware doing hot module replacement (HMR), so the middleware uses a websocket to push changes to the client. But my application code also needs its own websocket connection between…
Thijs Koerselman
  • 21,680
  • 22
  • 74
  • 108
21
votes
3 answers

Display a static html file with koa.js

What I want to do is serve the index.html file when the index route (i.e. localhost:3000) is called. I use koa-router for routing, so my route looks like this: app.all("/", function * (next){ //Send the file here }); I tried to use koa-static…
tomet
  • 2,416
  • 6
  • 30
  • 45
19
votes
2 answers

Is using Joi for validation on top of Mongoose good practice?

I'm developing a RESTful API with Node.js, Mongoose and Koa and I'm a bit stuck on what are the best practices when it comes to schemas and input validation. Currently I have both a Mongoose and Joi schema for each resource. The Mongoose schema only…
rok
  • 557
  • 4
  • 20
19
votes
3 answers

forEach using generators in Node.js

I'm using Koa.js framework and Mongoose.js module. Normally to get a result from MongoDB I code like this: var res = yield db.collection.findOne({id: 'my-id-here'}).exec(); But I need to execute this line for every element of an array named…
Mazhar Ahmed
  • 1,523
  • 2
  • 24
  • 41
19
votes
3 answers

Any way to exports a generator function?

An example generator.js: exports.read = function *(){ var a = yield read('co.github.js'); var b = yield read('co.recevier.js'); var c = yield read('co.yield.js'); console.log([a,b,c]); } function read(file) { return function(fn){ …
Tinple
  • 1,261
  • 1
  • 11
  • 11
19
votes
2 answers

What are the differences between Koa and Express 4.0?

Koa and Express 4.0 are both fairly new, and from what I've read, Koa was made by the Express team. From what I understand, Koa requires features of node that are only available in 0.11 (the unstable branch) of node, and also uses generators.…
H Khan
  • 1,307
  • 3
  • 14
  • 20
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
18
votes
1 answer

What does star (*) mean in JavaScript function definition in Koa framework?

I have been familiarising myself with Koa (http://koajs.com/). Many of the examples include star character in place of function name. For instance in the hello world example there is: var koa = require('koa'); var app = koa(); app.use(function…
jsalonen
  • 29,593
  • 15
  • 91
  • 109
18
votes
4 answers

Request Body is undefined in KOA

I have KOA Like below : var koa = require('koa'), bodyParser = require('koa-body-parser'), router = require('koa-router'), app = koa(); app.use(router(app)); app.use(bodyParser()); app.post('http://localhost/get',getit); function *getit(){ …
MBehtemam
  • 7,865
  • 15
  • 66
  • 108
1
2 3
82 83