node.js - "OR" operator in regex, in express routing -


i want both "/" , "/home" path, direct user same page("/home").
how can write regex in express routing. tried this:
router.route('/|/home')
dose not work.

you may use

router.route(/^\/(home)?$/) 

the regex matches:

  • ^ - start of string
  • \/ - slash
  • (home)? - optional sequence of literal chars home
  • $ - end of string.

basically, same /^(\/|\/home)$/ 2 alternatives, \/ , \/home both anchored @ start , end of string, optional group (i.e. (home)?) more optimal way match 2 alternatives.

note non-capturing group can used here (and preferred), tiny bit less readable: /^\/(?:home)?$/.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -