Skip to content

Middleware

Björn Schmidtke edited this page May 19, 2017 · 2 revisions

Penguin.js makes it easy to include your own middleware.

penguin run and penguin serve accept an argument like --middleware [ ./custom ].

Penguin.js excpects that the middleware exports a express middleware.

//custom.js
'use strict'
const { Router } = require('express')
const request = require('request')
const noCache = require('nocache')


module.exports = () => {
  const router = Router()

  router.get('/custom', noCache(), (req, res) => {
    res.send(`Hello World`)
  })

  return router
}

Notice: If you include multiple middlewares, the order of the middlewares will be the same as the order of the arguments passed to penguin!

Example 1: Let's say you have two middlewares that you include like that:

penguin run --middleware [ ./custom ] --middleware [ penguin-passwordless ...]

Here the middleware in custom will be included and accessed before the authentication happens. Meaning, it will be publicly accessible.

Example 2:

penguin run --middleware [ penguin-passwordless ...]  --middleware [ ./custom ]

Here the authentication will happen before. Meaning, to access custom, you have to authenticate first.

Clone this wiki locally