1

Here is my front-end AngularJS routing:

$urlRouterProvider.otherwise('/');
$locationProvider.html5Mode(true).hashPrefix('!');
$stateProvider
.state('main', {
    url: '/',
    templateUrl: '/views/main.html',
    controller: 'MainCtrl'
})

.state('review', {
    url: '/review',
    templateUrl: '/views/review.html',
    controller: 'NewReviewCtrl'
})

.state('model', {
    url: '/:make/:model',
    templateUrl: '/views/model.html',
    controller: 'ModelPageCtrl'
});

And here is my server-side routing (node.js):

//...some routes

app.use('/*', function(req, res){
    res.sendFile(config.rootPath + '/public/index.html');
});

With /review route, everything works fine, but not with /:make/:model:

HTML:

<a href="" ui-sref="model({make: 'lenovo', model: 'thinkpad-t430'})">See more details...</a>

If I go to this page through that link, everything works fine, but if I refresh or go directly to localhost:3030/lenovo/thinkpad-t430 I get this error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app to:
Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.  

It is probably because of the html5mode. And I suggest it not working with this URL because, it is a 2 level URL.

How can I fix this?

Edit: Express.js config:

app.set('view engine', 'ejs');
app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});
app.use(bodyParser());
app.use(session({secret:"some_secret"}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(config.rootPath+"/public"));
5
  • please post your main html file and the module configuration. seems like problem with it. Commented Dec 29, 2014 at 15:34
  • It look like problem with module initialization, please provide your code related t module config Commented Dec 29, 2014 at 15:41
  • there isn't any other module config Commented Dec 29, 2014 at 15:44
  • Can you replicate the issue in a plunker? Here is a blank template you can fork: plnkr.co/edit/WprgipAdFBtxDKM6jogo?p=info Commented Dec 29, 2014 at 16:11
  • you have ng-app declared somewhere right? Commented Dec 29, 2014 at 16:12

1 Answer 1

1

I created working plunker, which does use the same UI-Router setting as mentioned above. Everything is working. So, we should know now, that the Client side should be set properly.

The main and only difference is, more explicit setting, which allows us to omit <base href="/" /> in the head

$locationProvider
  .html5Mode(
  {
    enabled: true, 
    requireBase: false,
  })

Or we can use the <base> element (check the index.html <head>) int that plunker

Seems like the issue is on the server side, please check twice:

How to: Configure your server to work with html5Mode

Sign up to request clarification or add additional context in comments.

3 Comments

That is really surprising. Could you please, try to check if requireBase: true and explicit <base href="/" /> is working? if yes, then we should just play a bit with <link> elements.... I updated my answer and included <link href="style.css" rel="stylesheet" /> there...
I fixed it by putting <base> before the <link>' tags
Great job sir! enjoy amazing UI-Router ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.