Search code examples
javascriptnode.jsexpresswebserver

Get /index.html


So I'm using node.js to serve a express webserver.

I'm able to catch https://my.site.com/information:

express.use('/information',function(req,res) {

})

but I'm not able to catch https://my.site.com/information.html like this:

express.get('/information.html',function(req,res) {
  // not working
})

How can I get one catch to get information AND information.html - maybe using regex or something like this.. I'm clueless.


Solution

  • Look at the documentation for get:

    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.

    So you can provide a regular expression for this:

    app.get(/\/information(?:\.html)?$/, (req, res) => res.send('Hello World!'))