diff --git a/examples/blog/index.js b/examples/blog/index.js new file mode 100644 index 0000000..c6d2eb0 --- /dev/null +++ b/examples/blog/index.js @@ -0,0 +1,92 @@ + +/** + * Module dependencies. + */ + +var render = require('./lib/render'); +var logger = require('koa-logger'); +var route = require('koa-route'); +var views = require('co-views'); +var koa = require('../..'); +var app = koa(); + +// "database" + +var posts = []; + +// middleware + +app.use(logger()); + +// route middleware + +app.use(route.get('/', list)); +app.use(route.get('/post/new', add)); +app.use(route.get('/post/:id', show)); +app.use(route.post('/post', create)); + +// route definitions + +/** + * Post listing. + */ + +function *list() { + this.body = yield render('list', { posts: posts }); +} + +/** + * Show creation form. + */ + +function *add() { + this.body = yield render('new'); +} + +/** + * Show post :id. + */ + +function *show(id) { + var post = posts[id]; + if (!post) this.throw(404, 'invalid post id'); + this.body = yield render('show', { post: post }); +} + +/** + * Create a post. + */ + +function *create() { + var post = yield parse(this); + var id = posts.push(post) - 1; + post.created_at = new Date; + post.id = id; + this.redirect('/'); +} + +// TODO: use a lib... + +var qs = require('querystring'); + +function parse(ctx) { + return function(done){ + var buf = ''; + var req = ctx.req; + req.setEncoding('utf8'); + req.on('data', function(chunk){ buf += chunk }); + req.on('end', function(){ + try { + done(null, qs.parse(buf)); + } catch (err){ + err.body = buf; + err.status = 400; + done(err); + } + }); + } +} + +// listen + +app.listen(3000); diff --git a/examples/blog/lib/render.js b/examples/blog/lib/render.js new file mode 100644 index 0000000..f018cac --- /dev/null +++ b/examples/blog/lib/render.js @@ -0,0 +1,13 @@ + +/** + * Module dependencies. + */ + +var views = require('co-views'); + +// setup views mapping .html +// to the swig template engine + +module.exports = views(__dirname + '/../views', { + map: { html: 'swig' } +}); \ No newline at end of file diff --git a/examples/blog/views/index.html b/examples/blog/views/index.html new file mode 100644 index 0000000..392e0ba --- /dev/null +++ b/examples/blog/views/index.html @@ -0,0 +1,9 @@ + + + Blog + + + {% for post in posts %} + {% endfor %} + + \ No newline at end of file diff --git a/examples/blog/views/layout.html b/examples/blog/views/layout.html new file mode 100644 index 0000000..2d2d886 --- /dev/null +++ b/examples/blog/views/layout.html @@ -0,0 +1,62 @@ + + + {% block title %}Blog{% endblock %} + + + +
+ {% block content %} +

Missing content!

+ {% endblock %} +
+ + \ No newline at end of file diff --git a/examples/blog/views/list.html b/examples/blog/views/list.html new file mode 100644 index 0000000..5146be3 --- /dev/null +++ b/examples/blog/views/list.html @@ -0,0 +1,17 @@ +{% extends 'layout.html' %} + +{% block title %}Posts{% endblock %} + +{% block content %} +

Posts

+

You have {{ posts.length }} posts!

+

Create a Post

+ +{% endblock %} \ No newline at end of file diff --git a/examples/blog/views/new.html b/examples/blog/views/new.html new file mode 100644 index 0000000..5907d9e --- /dev/null +++ b/examples/blog/views/new.html @@ -0,0 +1,13 @@ +{% extends 'layout.html' %} + +{% block title %}New Post{% endblock %} + +{% block content %} +

New Post

+

Create a new post.

+
+

+

+

+
+{% endblock %} \ No newline at end of file diff --git a/examples/blog/views/show.html b/examples/blog/views/show.html new file mode 100644 index 0000000..d06164e --- /dev/null +++ b/examples/blog/views/show.html @@ -0,0 +1,8 @@ +{% extends 'layout.html' %} + +{% block title %}{{ post.title }}{% endblock %} + +{% block content %} +

{{ post.title }}

+

{{ post.body }}

+{% endblock %} \ No newline at end of file diff --git a/package.json b/package.json index 3771815..7331c68 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,9 @@ "ejs": "~0.8.4", "koa-logger": "~1.0.1", "koa-static": "~1.2.0", - "co-busboy": "git://github.com/cojs/busboy" + "co-busboy": "git://github.com/cojs/busboy", + "koa-route": "~1.0.2", + "swig": "~1.1.0" }, "engines": { "node": "> 0.11.4"