hello.js 595 B

123456789101112131415161718192021
  1. function routeFactory({ db, yup, log }) {
  2. return {
  3. verb: 'get',
  4. path: '/hello/:name',
  5. handler(req, res, next) {
  6. const contentPath = ['visits', req.params.name];
  7. db.get(contentPath, reply => {
  8. const visits = reply >= 0 ? Number(reply) + 1 : 1;
  9. log.info(`Visited by ${req.params.name}.`);
  10. res.send(`hello ${req.params.name}. You have visited ${visits} times.`);
  11. next();
  12. db.set(contentPath, visits);
  13. });
  14. },
  15. schema: yup.object().shape({
  16. name: yup.string().required(),
  17. }),
  18. };
  19. }
  20. export default routeFactory;