From 36febf0e3b132859c5ad5e9196ae15f425813163 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sat, 7 Mar 2015 09:23:59 +0300 Subject: [PATCH] news route update; + /news/; mime routers --- routes/news.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/routes/news.js b/routes/news.js index bf4dd4d3..16b3dcb9 100644 --- a/routes/news.js +++ b/routes/news.js @@ -3,10 +3,39 @@ */ var route = require("koa-route"); +var router = require("./"); var db = require("../core/db"); module.exports = function (app) { - app.use(route.get("/news", function *(){ - this.body = yield db.c("posts").find().toArray(); - })); + app.use(route.get("/news", router.accepts({ + "text/html": function *(){ + this.redirect("/"); + }, + "text/plain": function *(){ + this.redirect("/"); + }, + "application/json": function *(){ + var count = parseInt(this.query["count"]) || 10; + var portion = parseInt(this.query["portion"]) || 0; + var skip = portion * count; + + var query = {}; + if(this.query.type) query.type = this.query.type; + + this.body = yield db.c("posts").find(query).skip(skip).limit(count).toArray(); + } + }))); + + app.use(route.get("/news/:uri", router.accepts({ + "text/html": router.index(), + "text/plain": router.index(), + "application/json": function *(uri){ + var item = yield db.c("posts").findOne({uri: uri}); + if(item){ + this.body = item; + }else { + this.status = 404; + } + } + }))); }; \ No newline at end of file