This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Module dependencies. | |
| */ | |
| var logger = require('koa-logger'); | |
| var serve = require('koa-static'); | |
| var parse = require('co-busboy'); | |
| var koa = require('koa'); | |
| var fs = require('fs'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var app = require('./app'); | |
| var co = require('co'); | |
| var should = require('should'); | |
| var request = require('supertest').agent(app.listen()); | |
| var users = app.users; | |
| describe('CRUD Api for storing stuff in MongoDb with monk and co-monk', function(){ | |
| var removeAll = function(done){ | |
| co(function *(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create the application | |
| var koa = require('koa'); | |
| var route = require('koa-route'); | |
| var parse = require('co-body'); | |
| var app = module.exports = koa(); | |
| // Set up monk stuff, via co-monk | |
| var monk = require('monk'); | |
| var wrap = require('co-monk'); | |
| var db = monk('localhost/koa_users'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var koa = require('koa'); | |
| var app = module.exports = koa(); | |
| var tobi = { | |
| _id: '123', | |
| name: 'tobi', | |
| species: 'ferret' | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var auth = require('koa-basic-auth'); | |
| var koa = require('koa'); | |
| var app = koa(); | |
| // custom 401 handling | |
| app.use(function *(next){ | |
| try { | |
| yield next; | |
| } catch (err) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var app = require('./app'); | |
| var request = require('supertest').agent(app.listen()); | |
| describe('Errors', function () { | |
| it('should catch the error', function(done){ | |
| request | |
| .get('/') | |
| .expect(500) | |
| .expect('Content-Type', /text\/html/, done); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var koa = require('koa'); | |
| var app = module.exports = koa(); | |
| // look ma, error propagation! | |
| app.use(function *(next){ | |
| try { | |
| yield next; | |
| } catch (err) { | |
| // some errors will have .status | |
| // however this is not a guarantee |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Module dependencies. | |
| */ | |
| var render = require('./lib/render'); | |
| var logger = require('koa-logger'); | |
| var route = require('koa-route'); | |
| var views = require('co-views'); | |
| var parse = require('co-body'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // some requires are needed | |
| // route | |
| app.use(route.get('/user', list)); | |
| // And here is the handling code | |
| function *list() { | |
| var res = yield users.find({}); | |
| this.body = res; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| curl -X POST -H "Content-Type: application/json" -d '{"username":"marcusoft.net@gmail.com","password":"xyz"}' http://localhost:3000/user -v | |
| curl -X POST -H "Content-Type: application/json" -d '{"username":"hugo@gmail.com","password":"xyz^2"}' http://localhost:3000/user -v | |
| curl -X POST -H "Content-Type: application/json" -d '{"username":"abbe@gmail.com","password":"zyx"}' http://localhost:3000/user -v |