Ilja Ilja - 1 month ago 9
Node.js Question

Is there a way to check post to 1 url or the other in express?

I am currently proxying my session creation url with express in a way like this:

app.post('/sessions', (req, res) => {
// Logic here
})


Logic that goes into this proxy also repeats for my
/confirmation
endpoint, hence I would not want to rewrite it instead I'd want above
app.post(...
to also check for my second url, is this possible?

Answer

From the documentation for app.post

The path for which the middleware function is invoked; can be any of:

  • A string representing a path.
  • A path pattern.
  • A regular expression pattern to match paths.
  • An array of combinations of any of the above.

which means you can actually pass in an array of paths

app.post(['/sessions', '/confirmation'] , (req, res) => {
    // Logic here
});