Creating controller functions

Finally, we will create controller methods for the various actions we want our application to be able to carry out. These include the following:

  • Index: View all blog posts
  • Create: View a form to create a new blog post
  • Store: Save a new blog post to the database
  • Show: View a single blog post
  • Edit: View a form to edit a blog post
  • Update: Update a blog post in the database

Let's create a controller file for our post actions name PostController in the controllers folder, and insert the following contents into it:

// ./controllers/PostController.jsconst Post = require('../models/Post');module.exports = {  async index(ctx) {    const posts = await Post.find()    .populate('author');    ctx.state.posts = posts; ctx.state.title ...

Get Server Side development with Node.js and Koa.js Quick Start Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.