mirror of
https://github.com/koajs/examples.git
synced 2026-01-25 14:48:15 +00:00
73 lines
1.1 KiB
JavaScript
73 lines
1.1 KiB
JavaScript
|
|
const render = require('./lib/render');
|
|
const logger = require('koa-logger');
|
|
const router = require('@koa/router')();
|
|
const koaBody = require('koa-body');
|
|
|
|
const Koa = require('koa');
|
|
const app = module.exports = new Koa();
|
|
|
|
// "database"
|
|
|
|
const posts = [];
|
|
|
|
// middleware
|
|
|
|
app.use(logger());
|
|
|
|
app.use(render);
|
|
|
|
app.use(koaBody());
|
|
|
|
// route definitions
|
|
|
|
router.get('/', list)
|
|
.get('/post/new', add)
|
|
.get('/post/:id', show)
|
|
.post('/post', create);
|
|
|
|
app.use(router.routes());
|
|
|
|
/**
|
|
* Post listing.
|
|
*/
|
|
|
|
async function list(ctx) {
|
|
await ctx.render('list', { posts: posts });
|
|
}
|
|
|
|
/**
|
|
* Show creation form.
|
|
*/
|
|
|
|
async function add(ctx) {
|
|
await ctx.render('new');
|
|
}
|
|
|
|
/**
|
|
* Show post :id.
|
|
*/
|
|
|
|
async function show(ctx) {
|
|
const id = ctx.params.id;
|
|
const post = posts[id];
|
|
if (!post) ctx.throw(404, 'invalid post id');
|
|
await ctx.render('show', { post: post });
|
|
}
|
|
|
|
/**
|
|
* Create a post.
|
|
*/
|
|
|
|
async function create(ctx) {
|
|
const post = ctx.request.body;
|
|
const id = posts.push(post) - 1;
|
|
post.created_at = new Date();
|
|
post.id = id;
|
|
ctx.redirect('/');
|
|
}
|
|
|
|
// listen
|
|
|
|
if (!module.parent) app.listen(3000);
|