Regex for route matching in Express

app.get('/:type(discussion|page)/:id', ...) works


You should use a literal javascript regular expression object, not a string, and @sarnold is correct that you want parens for alternation. Square brackets are for character classes.

const express = require("express");
const app = express.createServer();
app.get(/^\/(discussion|page)\/(.+)/, function (req, res, next) {
  res.write(req.params[0]); //This has "discussion" or "page"
  res.write(req.params[1]); //This has the slug
  res.end();
});

app.listen(9060);

The (.+) means a slug of at least 1 character must be present or this route will not match. Use (.*) if you want it to match an empty slug as well.