Polka: Multiple middlewares and subapps

Created on 25 Sep 2018  路  4Comments  路  Source: lukeed/polka

Not sure if this one has been addressed but would like to share the problem I'm encountering.

It's a bit long to list everything out here but I will try my best. Alternatively, there is a demo for the said issue which can be found here: https://glitch.com/edit/#!/indigo-sneeze.

// app.js
const app = polka();

// middleware before subApp, but only `apiCore()` runs and `verifyToken` being skipped.
// It's like either one can be run at the same time.
polka.use('/api', verifyToken(), apiCore());
// verify-token.js

function verifyToken() {
  return (req, res, next) {
    ... // Verify token middleware
  }
}
// api-core.js
function apiCore() {
  return async (req, res) => {
    return polka() // subApp consists of multiple route handlers
      .post('/create', create())
      .post('/view', view());
  };
}

All 4 comments

Hey! Good timing haha, I _just_ fixed this tonight, along with a couple related issues.

I'll commit and release the changes tomorrow morning. Thanks!

Thank god (and @lukeed)! 馃槃 Then I will wait for the new release. Thank you so much! 馃帀

Lol not a problem.

In the meantime, you can move your verifyToken into your sub-app directly. The next release you can take it back out.

// api-core.js
module.exports = function apiCore() {
  // return async (req, res) => { REMOVE THIS LINE
    return polka()
      .use(verifyToken)
      .post('/create', create())
      .post('/view', view());
  // }; AND THIS
}

You should return the sub-app directly, always. Otherwise you are recreating the app every time.

Ok, bed time 馃憢

It is now working as expected. Thanks for the patch. 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paz-raon picture paz-raon  路  10Comments

motss picture motss  路  3Comments

kevinfiol picture kevinfiol  路  3Comments

Dindaleon picture Dindaleon  路  3Comments

ansarizafar picture ansarizafar  路  3Comments