MattDionis MattDionis - 2 years ago 125
Node.js Question

Angular 'Cannot GET' route on page refresh

I have a search function within my Angular app that when executed hits my API to grab results then redirects the user using

. Everything works great...until I try to reload the results page. When I do so I get a
Cannot GET /search/[search-term]


'use strict';

.config(function($routeProvider, $locationProvider) {

.when('/player/:playerId', {
templateUrl: 'player.html'
.when('/search/:searchTerm', {
templateUrl: '/app/gmSearchResults/gmSearchResults.html'

enabled: true,
requireBase: false,
rewriteLinks: false

relevant navbar.controller.js:

vm.getSearchResults = function(searchTerm) {
query: searchTerm
}).$promise.then(function(results) {
if (results.playerId) {
$location.url('/customer/' + results.playerId);
} else {
$location.url('/search/' + searchTerm);

Answer Source

Setup your express app:

app.route('/*').get(function(req, res) { 
    return res.sendFile(path.join(config.root, 'index.html')); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download