title: Express 4, NodeJS, AngularJS routing
body: <p>I am using Express 4 to host my AngularJS app on my backend, with Nginx as my frontend server. However html5 mode does not seem to work, as I will get a Cannot /GET error when I try to enter the page link (e.g. <code>http://localhost/login</code>) via the browser. Is there any routing configuration I need to do for my Express/Nginx? Here's my config code:</p>
  
  <p><strong>Express 4:</strong></p>
  
  <pre><code>var express = require('express'),
      app = express(),
      server = require('http').Server(app),
      bodyParser = require('body-parser'),
      db = require('./db'),
      io = require('./sockets').listen(server),
      apiRoutes = require('./routes/api'),
      webRoutes = require('./routes/web');
  
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.use('/api', apiRoutes);
  app.use(express.static(__dirname + '/public'));
  
  server.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
  });
  </code></pre>
  
  <p><strong>AngularJS:</strong></p>
  
  <pre><code>'use strict';
  var nodeApp = angular.module('nodeApp',['ngRoute']);
  
  nodeApp.config(function($routeProvider, $locationProvider, $controllerProvider) {
    $routeProvider.when('/', {
      templateUrl: 'partials/home.html'
    }).when('/login', {
      templateUrl: 'partials/login.html'
    });
    $locationProvider.html5Mode(true);
  
    nodeApp.controllerProvider = $controllerProvider;
  });
  </code></pre>
  
  <p><strong>Nginx:</strong></p>
  
  <pre><code># the IP(s) on which your server is running
  upstream test-app {
    server 127.0.0.1:3000;
  }
  
  # the nginx server instance
  server {
    listen 0.0.0.0:80;
    server_name test-app.cloudapp.net;
    access_log /var/log/nginx/test-app.log;
  
    # pass the request to the nodejs server with correct headers
    location / {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-Nginx-Proxy true;
  
      proxy_pass http://test-app/;
      proxy_redirect off;
    }
  }
  </code></pre>