javascript - Express JS router sometimes returns 404 and sometimes 200 -


i use angular 2 , express js. requests not css, images, js, video send index.html file. router code:

router.get(/\/(?!((.*\.html$)|(.*\.css$)|(.*\.mp4)|(.*\.woff)|(.*\.js$)|(.*\.map$)|(.*\.jpg$)|(.*\.jpeg$)|(.*\.png$)|(.*\.gif$))).+$/gmi,   ensureconnect.ensureloggedin({ redirectto: '/' }),   function(req, res) {     res       .set('content-type', 'text/html')       .sendfile(../dist/index.html);   }); 

for root router '/' works perfect, if try open page (e.g. '/product/am-0596157134') open page, returns 404 (cannot /product/am-0596157134)

so tried reload page twice , 1st reload returns 404 , 2nd 1 - returns 200. here log:

::ffff:127.0.0.1 - - [10/nov/2016:11:46:24 +0000] "get /product/am-0596157134 http/1.1" 404 34 "-" "mozilla/5.0 (macintosh; intel mac os x 10_12_0) applewebkit/537.36 (khtml, gecko) chrome/54.0.2840.71 safari/537.36"

::ffff:127.0.0.1 - - [10/nov/2016:11:46:25 +0000] "get /product/am-0596157134 http/1.1" 200 2299 "-" "mozilla/5.0 (macintosh; intel mac os x 10_12_0) applewebkit/537.36 (khtml, gecko) chrome/54.0.2840.71 safari/537.36"

any ideas why happens, , how fix it?

tl;dr: remove g flag regular expression.

when use /g, regular expression keeps internal state (stored in lastindex property) able find successive matches.

in case, state maintained between requests, first request match performed, last index gets updated, , when new request comes in, matching starts last index. when doesn't match (and won't), state reset , new request match again.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -