I have a AngularJS webpage using ui-router for routing on HTML5Mode, that it means that URL doesn't have to have "index.html#" neither '#'.
To achieve that I configured my express as ui-router wiki says:
app.use('/app', express.static(__dirname + '/../web/app'));
app.use('/css', express.static(__dirname + '/../web/css'));
app.use('/img', express.static(__dirname + '/../web/img'));
app.use('/libs', express.static(__dirname + '/../web/libs'));
app.use('/views', express.static(__dirname + '/../web/views'));
app.all('/*', function(req, res, next) {
res.sendFile(path.resolve(__dirname + '/../web/index.html'));
});
The important part is last line, all calls that doesn't match app/css/img... return index.html
It works, the problem comes because I have another rule to return public files:
// Return file
app.use('/file', express.static(__dirname + "/../public/", options));
In the angular app I use $http to get a public file (file name is a parameter on URL) and if file exists I do something with it:
var fileUrl = "http://mydomain/file/" + $stateParams.filename + '.png';
$http.get(fileUrl)
.then(
function(response){
if(response.status == 200){
// File exist
}
}
);
The problem is that even if file doesn't exist I always get a response with an status of 200. Express routing tried to return the file but as it doesn't exist it return index.html. That is what I think happens
Recap:
http://mydomain/file/fileThatExist.png Returs Image http://mydomain/file/fileThatDoesNotExit.png Returs index.html
I tried to use a regex but it had same effect
// All calls that doesn't contain 'file' string
/*
^ # Start of string
(?! # Assert that it's impossible to match...
file # file, followed by
$ # end of string
) # End of negative lookahead assertion
.* # Now match anything
*/
app.all(/^\/(?!file$).*/, function(req, res, next) {
res.sendFile(path.resolve(__dirname + '/../web/index.html'));
});
Any help is welcome.