Installations
npm install koa2
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 4
Node Version
4.4.7
NPM Version
3.10.6
Score
67
Supply Chain
95.5
Quality
72.5
Maintenance
50
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
894,037
Last Day
21
Last Week
117
Last Month
1,216
Last Year
20,095
GitHub Statistics
35,278 Stars
1,220 Commits
3,226 Forks
833 Watching
8 Branches
246 Contributors
Bundle Size
200.51 kB
Minified
39.78 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.0-alpha.7
Package Id
koa2@2.0.0-alpha.7
Size
14.48 kB
NPM Version
3.10.6
Node Version
4.4.7
Publised On
17 Sept 2016
Total Downloads
Cumulative downloads
Total Downloads
894,037
Last day
-4.5%
21
Compared to previous day
Last week
15.8%
117
Compared to previous week
Last month
-80.6%
1,216
Compared to previous month
Last year
-40.8%
20,095
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
24
Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.
Only methods that are common to nearly all HTTP servers are integrated directly into Koa's small ~570 SLOC codebase. This includes things like content negotiation, normalization of node inconsistencies, redirection, and a few others.
Koa is not bundled with any middleware.
Installation
Koa requires node v4.0.0 or higher for (partial) ES2015 support.
$ npm install koa@next
Hello koa
1const Koa = require('koa'); 2const app = new Koa(); 3 4// response 5app.use(ctx => { 6 ctx.body = 'Hello Koa'; 7}); 8 9app.listen(3000);
Getting started
- Kick-Off-Koa - An intro to koa via a set of self-guided workshops.
- Workshop - A workshop to learn the basics of koa, Express' spiritual successor.
- Introduction Screencast - An introduction to installing and getting started with Koa
Middleware
Koa is a middleware framework that can take 3 different kinds of functions as middleware:
- common function
- async function
- generatorFunction
Here is an example of logger middleware with each of the different functions:
Common function
1// Middleware normally takes two parameters (ctx, next), ctx is the context for one request, 2// next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion. 3 4app.use((ctx, next) => { 5 const start = new Date(); 6 return next().then(() => { 7 const ms = new Date() - start; 8 console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); 9 }); 10});
async functions (Babel required)
1app.use(async (ctx, next) => { 2 const start = new Date(); 3 await next(); 4 const ms = new Date() - start; 5 console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); 6});
GeneratorFunction
To use generator functions, you must use a wrapper such as co that is no longer supplied with Koa.
1app.use(co.wrap(function *(ctx, next) { 2 const start = new Date(); 3 yield next(); 4 const ms = new Date() - start; 5 console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); 6}));
Old signature middleware (v1.x) - Deprecated
Old signature middleware (v1.x) support will be removed in v3
Koa v2.x will try to convert legacy signature, generator middleware on app.use
, using koa-convert.
It is however recommended that you choose to migrate all v1.x middleware as soon as possible.
1// Koa will convert 2app.use(function *(next) { 3 const start = new Date(); 4 yield next; 5 const ms = new Date() - start; 6 console.log(`${this.method} ${this.url} - ${ms}ms`); 7});
You could do it manually as well, in which case Koa will not convert.
1const convert = require('koa-convert'); 2 3app.use(convert(function *(next) { 4 const start = new Date(); 5 yield next; 6 const ms = new Date() - start; 7 console.log(`${this.method} ${this.url} - ${ms}ms`); 8}));
Babel setup
For Node 4.0 and Babel 6.0 you can setup like this:
1$ npm install babel-register babel-plugin-transform-async-to-generator --save
1// set babel in entry file 2require('babel-register')({ 3 plugins: ['transform-async-to-generator'] 4});
Check out an example in koa's test.
Running tests
$ make test
Authors
See AUTHORS.
Community
- API documentation
- Badgeboard and list of official modules
- Examples
- Middleware list
- Wiki
- G+ Community
- Reddit Community
- Mailing list
- Guide
- FAQ
- ä¸æ–‡æ–‡æ¡£
- #koajs on freenode
License
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/koajs/.github/SECURITY.md:1
- Info: Found linked content: github.com/koajs/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/koajs/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/koajs/.github/SECURITY.md:1
Reason
6 commit(s) and 5 issue activity found in the last 90 days -- score normalized to 9
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
Reason
Found 17/27 approved changesets -- score normalized to 6
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/koa/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/koajs/koa/node.js.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.js.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 23 are checked with a SAST tool
Score
6
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More