Coder Social home page Coder Social logo

workshop's Introduction

# 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)

workshop's People

Contributors

felixsanz avatar fengmk2 avatar hemanth avatar jonathanong avatar koba04 avatar t3chnoboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

workshop's Issues

Expected "Location" header field

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?

06-content-negotiation, "content-type" required when encoding is "gzip"

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');
It takes me a lot time to understand what this chapter is focusing on.

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;
  }
});

Question: 04-bodies

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

test case fails

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.