Skip to content

Commit

Permalink
Bad is good
Browse files Browse the repository at this point in the history
  • Loading branch information
vimukthi wickramasinghe authored and vimukthi wickramasinghe committed Jun 5, 2016
1 parent a6d976b commit 09c196e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
*.log
48 changes: 43 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -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')));
Expand Down
44 changes: 0 additions & 44 deletions controller.js

This file was deleted.

30 changes: 0 additions & 30 deletions test/routeSpec.js

This file was deleted.

0 comments on commit 09c196e

Please sign in to comment.