0

I want to use angularjs routing, I'm using but it's making extra requests in server side. Anyone know the solution of this problem, or I'm doing something wrong?

Client app.js

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $locationProvider.html5Mode({enabled: true, requireBase: false})
    $routeProvider.
        when('/', {
            templateUrl: '/tpl/main.tmp.html',
            controller: 'MainCtrl'
        })
        .otherwise({redirectTo: '/'})
}])

//routes.js
app.get('/', function(req, res) {
    console.log("test")
    res.render(__dirname+'/public/tpl/index.html', siteConfig)
})
//output
//test
//test
//test
//test

Files:

models
public
|-css
|-js
|--app.js
|--angular.js
app.js
Lazy
  • 1,807
  • 4
  • 29
  • 49
  • you get 4 server calls for a single page load? – Claies Mar 06 '15 at 20:52
  • Yes, I'm trying this with brand new project. – Lazy Mar 06 '15 at 20:56
  • is your page actually rendering? I don't see anything here that would cause you to be hitting the server `/` 4 times in one page load, unless it's hitting it for ALL of your server calls, i.e. calls for `.js`, `.css`, etc.... – Claies Mar 06 '15 at 21:00
  • I'have 4 css and 4 js file but angularjs working, page rendering successfully. I tried it without css files too and just with angularjs-angular-route, still same. – Lazy Mar 06 '15 at 21:02
  • do you get the same result without the use of html5Mode? – Claies Mar 06 '15 at 21:04
  • something is missing here.... There is no reason for angular to make 4 requests to the server for one page, unless there is something on the page making requests that isn't shown here in your snippets. – Claies Mar 06 '15 at 21:07
  • Yeah.. Very interesting problem. But I deleted one of the js files, it showed three logs in console, I didn't notice this before. – Lazy Mar 06 '15 at 21:09
  • I deleted without ngRoute, still same, I should made something wrong. – Lazy Mar 06 '15 at 22:08
  • Since you are already using angular routing then why are you again trying to render the same page from express, you don't need that. You should assume that with AngularJS in front, Express is not less than a REST engine, you should expose the API and consume it in front end. – NarendraSoni Mar 07 '15 at 08:04
  • I'm rendering index.html, it calls app.js, then it's rendering main.tmp.html, I don't think this is wrong? – Lazy Mar 07 '15 at 13:05

1 Answers1

0

A few things may cause this, a closer inspection of both request packets might narrow down the cause. Some ideas to check for:

Community
  • 1
  • 1
seanhodges
  • 17,426
  • 15
  • 71
  • 93