diff --git a/.gitignore b/.gitignore index 3c3629e..eb03e3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +*.log diff --git a/app.js b/app.js index becb7ff..c8fcfcf 100644 --- a/app.js +++ b/app.js @@ -1,20 +1,58 @@ 'use strict'; -const constroller = require('./controller'); const compress = require('koa-compress'); const logger = require('koa-logger'); const serve = require('koa-static'); const route = require('koa-route'); const koa = require('koa'); const path = require('path'); + +// controller dependancies +const views = require('co-views'); +const parse = require('co-body'); + +let messages = [{ + id: 0, + message: 'Koa next generation web framework for node.js' +}, { + id: 1, + message: 'Koa is a new web framework designed by the team behind Express' +}]; + +const render = views(__dirname + '/views', { + map: { + html: 'swig' + } +}); + const app = module.exports = koa(); // Logger app.use(logger()); -app.use(route.get('/', constroller.home)); -app.use(route.get('/messages', constroller.list)); -app.use(route.get('/messages/:id', constroller.fetch)); -app.use(route.post('/messages', constroller.create)); +app.use(route.get('/', function* home(ctx) { + let msgs = messages.slice(); + msgs.reverse(); + this.body = yield render('list', { + 'messages': msgs + }); +})); +app.use(route.get('/messages', function* list() { + this.body = yield messages; +})); +app.use(route.get('/messages/:id', function* fetch(id) { + const message = messages[id]; + if (!message) { + this.throw(404, 'message with id = ' + id + ' was not found'); + } + this.body = yield message; +})); +app.use(route.post('/messages', function* create() { + const message = yield parse(this); + const id = messages.push(message) - 1; + messages.shift(); + message.id = id; + this.redirect('/'); +})); // Serve static files app.use(serve(path.join(__dirname, 'public'))); diff --git a/controller.js b/controller.js deleted file mode 100644 index d80167d..0000000 --- a/controller.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; -const views = require('co-views'); -const parse = require('co-body'); -let messages = [{ - id: 0, - message: 'Koa next generation web framework for node.js' -}, { - id: 1, - message: 'Koa is a new web framework designed by the team behind Express' -}]; - -const render = views(__dirname + '/../views', { - map: { - html: 'swig' - } -}); - -module.exports.home = function* home(ctx) { - let msgs = messages.slice(); - msgs.reverse(); - this.body = yield render('list', { - 'messages': msgs - }); -}; - -module.exports.list = function* list() { - this.body = yield messages; -}; - -module.exports.fetch = function* fetch(id) { - const message = messages[id]; - if (!message) { - this.throw(404, 'message with id = ' + id + ' was not found'); - } - this.body = yield message; -}; - -module.exports.create = function* create() { - const message = yield parse(this); - const id = messages.push(message) - 1; - messages.shift(); - message.id = id; - this.redirect('/'); -}; diff --git a/test/routeSpec.js b/test/routeSpec.js deleted file mode 100644 index 269681b..0000000 --- a/test/routeSpec.js +++ /dev/null @@ -1,30 +0,0 @@ -/*global describe, it*/ -'use strict'; -const superagent = require('supertest'); -const app = require('../app'); -const request = superagent(app.listen()); - -describe('Routes', () => { - describe('GET /', () => { - it('should return 200', done => { - request - .get('/') - .expect(200, done); - }); - }); - describe('GET /messages', () => { - it('should return 200', done => { - request - .get('/messages') - .expect('Content-Type', /json/) - .expect(200, done); - }); - }); - describe('GET /messages/notfound', () => { - it('should return 404', done => { - request - .get('/messages/notfound') - .expect(404, done); - }); - }); -});