I use angular 2 and express js. So for all requests which is not css, images, js, video I send index.html file. My 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 '/' it works perfect, but if I try open another page (e.g. '/product/am-0596157134') it sometimes open page, but sometimes returns 404 (Cannot GET /product/am-0596157134)
So I tried to reload page twice and for the 1st reload it returns 404 and for the 2nd one - it returns 200. Here is 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, like 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, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
Any ideas why it happens, and how to fix it?
/product/*
) that all "Angular" requests will match? Using such a large regex seems overly complicated to me, and difficult to maintain. – robertklep