From 5e74f73c938f321e5fc1c7217b8de7ff8e5380a2 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Mon, 30 Dec 2013 22:23:04 -0800 Subject: [PATCH] examples: remove a few already migrated with tests --- examples/negotiation.js | 80 ------------------------------------- examples/templates.js | 38 ------------------ examples/templates/user.ejs | 1 - 3 files changed, 119 deletions(-) delete mode 100644 examples/negotiation.js delete mode 100644 examples/templates.js delete mode 100644 examples/templates/user.ejs diff --git a/examples/negotiation.js b/examples/negotiation.js deleted file mode 100644 index 9570b6a..0000000 --- a/examples/negotiation.js +++ /dev/null @@ -1,80 +0,0 @@ - -var koa = require('..'); -var app = koa(); - -var tobi = { - _id: '123', - name: 'tobi', - species: 'ferret' -}; - -var loki = { - _id: '321', - name: 'loki', - species: 'ferret' -}; - -var users = { - tobi: tobi, - loki: loki -}; - -// content negotiation middleware. -// note that you should always check for -// presence of a body, and sometimes you -// may want to check the type, as it may -// be a stream, buffer, string, etc. - -app.use(function *(next){ - yield next; - - // responses vary on accepted type - this.vary('Accept'); - this.status = 'bad request'; - - // no body? nothing to format, early return - if (!this.body) return; - - // accepts json, koa handles this for us, - // so just return - if (this.accepts('json')) return; - - // accepts xml - if (this.accepts('xml')) { - this.type = 'xml'; - this.body = '' + this.body.name + ''; - return; - } - - // accepts html - if (this.accepts('html')) { - this.type = 'html'; - this.body = '

' + this.body.name + '

'; - return; - } - - // default to text - this.body = this.body.name; -}); - -// filter responses, in this case remove ._id -// since it's private - -app.use(function *(next){ - yield next; - - if (!this.body) return; - - delete this.body._id; -}); - -// try $ GET /tobi -// try $ GET /loki - -app.use(function *(){ - var name = this.path.slice(1); - var user = users[name]; - this.body = user; -}); - -app.listen(3000); diff --git a/examples/templates.js b/examples/templates.js deleted file mode 100644 index 5a42356..0000000 --- a/examples/templates.js +++ /dev/null @@ -1,38 +0,0 @@ - -var views = require('co-views'); -var koa = require('..'); -var app = koa(); - -// setup views - -var render = views('examples/templates', { - ext: 'ejs' -}); - -// dummy data - -var user = { - name: { - first: 'Tobi', - last: 'Holowaychuk' - }, - species: 'ferret', - age: 3 -}; - -// logger - -app.use(function *logger(next){ - var start = new Date; - yield next; - var ms = new Date - start; - console.log('%s %s - %s', this.method, this.url, ms); -}); - -// render - -app.use(function *(){ - this.body = yield render('user', { user: user }); -}) - -app.listen(4000); diff --git a/examples/templates/user.ejs b/examples/templates/user.ejs deleted file mode 100644 index cb64c1e..0000000 --- a/examples/templates/user.ejs +++ /dev/null @@ -1 +0,0 @@ -

<%= user.name.first %> is a <%= user.age %> year old <%= user.species %>.

\ No newline at end of file