Pre-routing with querystrings with Express in Node JS

后端 未结 2 779
借酒劲吻你
借酒劲吻你 2020-12-19 06:35

I\'m trying to use express to parse the querystring in case certain parameters are set and execute a little piece of code, before the actual routing is happening. The use-ca

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 07:05

    Ok, there is quite a logical flaw in here. Routing only uses the URL, and ignores the querystring.

    The (or better "A") solution is actually this:

    app.get('*', function(req, res, next) {
      if (req.query.something) {
        console.log("Something: "+req.query.something);
      };
      next();
    })
    

    Explanation: As Express is ignoring the querystring for the routing, the only regular expression matching all URL's is "*". Once that is triggered, I can check if said querystring is existing, do my logic and continue the routing matching the next rule by using "next()".

    And yes: facepalm

提交回复
热议问题