redbrick/static-site

View on GitHub
routes/posts.js

Summary

Maintainability
A
0 mins
Test Coverage
const express = require('express');
const router = express.Router();
const getLatestPosts = require('../lib/getLatestPosts');

/* fetches latest blog posts as JSON list
 * optional query params:
 *  - offset (0-indexed starting point - default 0)
 *  - limit (0-indexed maximum number of returned results - default 10)
 *  - include (comma-separated list possibly including 'content,excerpt')
 */
router.get('/posts', ({ query }, res) => {
  getLatestPosts({
    offset : parseInt(query.offset),
    limit  : parseInt(query.limit),
    include: (query.include || '').split(','),
  }).then((posts) => {
    res.json(posts).end();
  })
  .catch((err) => {
    return res.status(500).json(err).end();
  });
});

module.exports = router;