Ashish308 Ashish308 -4 years ago 159
AngularJS Question

How do I get pretty URLs (remove #) while using ui-router.?

I have already tried $locationProvider and base href tag in my head of my index.html file. But was getting 404 error while rendering my views. I think it is something with my app.use method of Express in app.js file.

This is my folder structure of this project.

This is my app.js file.

var express = require('express');
var app = express();
console.log('Running on port 3000...');

This is my script.js file.

var myApp = angular.module('myApp', ['ui.router']);
myApp.config(function($stateProvider,$urlRouterProvider) {
.state('home', {
url: '/',
templateUrl : 'views/home.html',
controller : 'homeController'
.state('posts', {
url: '/posts',
templateUrl : 'views/posts.html',
controller : 'postsController'
.state('radio', {
url: '/radio',
templateUrl : 'views/radio.html',
controller : 'radioController'
myApp.controller('homeController', function($scope) {
$scope.title = 'Hello.';
myApp.controller('radioController', function($scope) {
$scope.message = 'Look! I am an radio page.';

This is my index.html file.

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="//" />
<link rel="stylesheet" href="styles/styles.css" />
<script src=""></script>
<script src="//"></script>
<script src="//"></script>
<script src="script.js"></script>
<body ng-app="myApp">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<a class="navbar-brand" href="/">Brand</a>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a ui-sref="home">HOME</a></li>
<li><a ui-sref="posts">POSTS</a></li>
<li><a ui-sref="radio">RADIO</a></li>
<div class="container">
<div id="main">
<div ui-view></div>

Answer Source

In Angular place $locationProvider.html5Mode(true); in your myApp.config block.

Then for express add this before app.listen(3000)

app.all('/*', function(req, res) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download