koajs / workshop Goto Github PK
View Code? Open in Web Editor NEWKoa Training Workshop
License: MIT License
Koa Training Workshop
License: MIT License
# koa In this workshop, you will learn the basics of [koa](https://github.com/koajs/koa), [Express'](https://github.com/visionmedia/express) spiritual successor. This workshop was created for the [2014 LXJS conference in Lisbon, Portugal](https://github.com/lxjs/training-koa), and will be continuously maintained. If you have any questions or have suggestions for additional exercises, please let us know! ## Resources This workshop assumes you've had experience with: - node.js - Express, Restify, or any similar node.js frameworks - Asynchronous programming - callbacks or promises ES6 generators are relatively new. This training will only give you a superficial look into generators. You should read these resources before and after doing this workshop: - [co](https://github.com/visionmedia/co) - the control flow engine used by koa - [koajs](http://koajs.com) - the official web page for koa - [koa examples](https://github.com/koajs/examples) - a lot of examples for koa - http://www.jongleberry.com/koa.html - some blog posts on koa ## Instructions Install node 0.11.13+. Using `nvm`, you can install it like this: ```bash nvm install 0.11.13 nvm use 0.11.13 ``` You can also install it directly from http://nodejs.org or using various other node version managers such as [n](https://github.com/visionmedia/n). Then you must install this workshop. You can either fork this workshop (recommended) or clone it: ```bash git clone git://github.com/koajs/workshop cd workshop ``` Then install all the dependencies: ```bash npm install npm install -g mocha ``` Go through each training, which are stored in folders, in numerical older. Read the `README` file, edit the `index.js` files, then run each test by executing the following: ```bash mocha --harmony-generators test.js ``` If you get an error message like `SyntaxError: Unexpected token *`, this means you didn't run the the process with `--harmony-generators`. ## Learning more than just Koa Although writing tests is not part of this workshop, you should still learn how they work. An important part of creating apps is creating the tests for it. Inspect the `test.js` files and see how [supertest](https://github.com/visionmedia/supertest) and [mocha](https://github.com/visionmedia/mocha) are used, both of which are used in Koa, Express, and libraries and frameworks. You will also see what is expected from your sample apps by reading the tests. After you finish each training, you may want to `git commit` so you have a history of what you've done: ```bash git commit -a -m "i finished training 1!" ``` Many small commits is good practice! ## Contact * [Jonathan Ong](https://github.com/jonathanong) - [@jongleberry](https://twitter.com/jongleberry)
Make a node school version workshop?
Maybe I can help, but my english sucks. :D
Asynchronous Generators as Middleware - a high level look into Koa
the link is broken http://cdn.rawgit.com/jonathanong/koajs/master/index.html
I am at 10-authentication, trying to get past the test that says "POST /login should 303 with good auth details"
/**
* If successful, the logged in user should be redirected to `/`.
*/
app.use(function* login(next) {
if (this.request.path !== '/login') return yield* next;
if (this.request.method === 'GET') return this.response.body = form.replace('{{csrf}}', this.csrf);
if (this.request.method === 'POST') {
var body = yield parse.json(this);
if (body.username !== 'username' || body.password !== 'password') {
return this.response.status = 400;
}
if (body._csrf !== this.csrf) {
return this.response.status = 403;
}
if (body.username === 'username' && body.password === 'password' && body._csrf === this.csrf) {
this.response.status = 303;
this.response.set('Location', '/');
}
}
})
here is the test result
1) Authentication logging in POST /login should 303 with good auth details:
Error: expected "Location" header field
at Test.assert (/Users/christiansakai/Desktop/workshop/node_modules/supertest/lib/test.js:190:35)
at assert (/Users/christiansakai/Desktop/workshop/node_modules/supertest/lib/test.js:132:12)
at /Users/christiansakai/Desktop/workshop/node_modules/supertest/lib/test.js:129:5
at Test.Request.callback (/Users/christiansakai/Desktop/workshop/node_modules/supertest/node_modules/superagent/lib/node/index.js:746:30)
at Test.<anonymous> (/Users/christiansakai/Desktop/workshop/node_modules/supertest/node_modules/superagent/lib/node/index.js:135:10)
at IncomingMessage.<anonymous> (/Users/christiansakai/Desktop/workshop/node_modules/supertest/node_modules/superagent/lib/node/index.js:938:12)
at endReadableNT (_stream_readable.js:893:12)
Can you point me in the right direction?
When accept-encoding
is gzip
, we need to set the content-type
to text/plain
.
this.response.set('Content-Encoding', 'gzip');
this.response.body = yield gzip('hello world');
The above code would fail the test because, when content-enconding
is gzip
, the default content-type
is application/octet-stream
.
Adding the content-type
assignment would solve the problem.
this.response.set('Content-Encoding', 'gzip');
this.response.set('content-type', 'text/plain');
this.response.body = yield gzip('hello world');
Here is one solution in case anyone need.
app.use(function* () {
const ae = this.request.acceptsEncodings('gzip', 'identity');
switch (ae) {
case 'gzip':
this.response.set('Content-Encoding', 'gzip');
this.response.set('content-type', 'text/plain');
this.response.body = yield gzip('hello world');
break;
case 'identity':
this.response.set('Content-Encoding', 'identity');
this.response.body = 'hello world';
break;
default:
break;
}
});
Hello,
I am trying to do the bonus problem in 04-bodies. Here is my code:
/**
* Create the `GET /stream` route that streams this file.
* In node.js, the current file is available as a variable `__filename`.
*/
app.use(function* (next) {
const stat = function (filename) {
return done => {
fs.stat(filename, (err, stats) => {
done(err, stats);
});
}
};
if (this.request.path !== '/stream') return yield* next;
this.response.type = 'application/javascript';
this.response.length = yield stat(__filename);
this.response.body = fs.createReadStream(__filename);
});
It is failing with this error log
Bodies
1) GET /stream should return a stream
โ GET /json should return a JSON body
1 passing (44ms)
1 failing
1) Bodies GET /stream should return a stream:
Error: Parse Error
at Error (native)
at Socket.socketOnData (_http_client.js:305:20)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
I think my this.response.length
is wrong
I just ran the Hello world module of the webshop. The simplest test case failed with error Error: expected "Content-Length" of "11", got "11"
. The complete error log mentioned below.
Hello World
1) should return hello world
0 passing (24ms)
1 failing
1) Hello World
should return hello world:
Error: expected "Content-Length" of "11", got "11"
at Test._assertHeader (D:\projects\techonology\node\koa\workshop\node_modules\supertest\lib\test.js:247:12)
at Test._assertFunction (D:\projects\techonology\node\koa\workshop\node_modules\supertest\lib\test.js:281:11)
at Test.assert (D:\projects\techonology\node\koa\workshop\node_modules\supertest\lib\test.js:171:18)
at assert (D:\projects\techonology\node\koa\workshop\node_modules\supertest\lib\test.js:131:12)
at D:\projects\techonology\node\koa\workshop\node_modules\supertest\lib\test.js:128:5
at Test.Request.callback (D:\projects\techonology\node\koa\workshop\node_modules\superagent\lib\node\index.js:706:12)
at IncomingMessage.parser (D:\projects\techonology\node\koa\workshop\node_modules\superagent\lib\node\index.js:906:18)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.