Wildcard or regex node express mount path on middleware

I would like to define other middleware depending on how the path looks. enter image description here

The fact is that the path can be careful; I would like, for example, to support the following paths:

/chat/auth/test
/chat/auth/add
/chrome/auth/test
/chrome/add

Every time auth is on the way, I would like the middleware name to be called, and for chat and chrome I want their respective intermediaries to be called.

app.js:

// Middleware authentication

var chatAuthenticate = require('./server/middleware/chat-authenticate');
app.use('/chat', chatAuthenticate(addon));

var authAuthentication = require('./server/middleware/auth-authenticate');
app.use('/auth', authAuthentication());

, app.js , /chat/auth /chrome/auth, , , :)

+4
1
app.use(/auth/, authAuthentication);

authAuthentication , auth . , :

  • , /.../
  • , , (, /chat/ RegEx, /auth/chat /chat/auth), . app.use().
  • (request, response, next) authAuthentication. , , , , - .
  • require script
  • , -. .
+1

Source: https://habr.com/ru/post/1625474/


All Articles