0

I have an Angular app running on Express 4.0 and I would like to send some information to the front end on startup, when the index file is served but I didn't manage to figure out on which route I should do this.

Thanks!

Bianca
  • 382
  • 4
  • 12

3 Answers3

1

I think you are looking for angular.module('...').run(). Here's the documentation on modules in angular. See the section on Module Loading & Dependencies. Also note you can only inject instances (not Providers) into .config and .run blocks.

Josh C.
  • 4,303
  • 5
  • 30
  • 51
  • Thanks for your input! The run block is what I'm currently using and I am doing a GET request to an express route to get the info needed to decorate the subsequent requests but I was wondering if there wouldn't be a way to decorate the very first request too. – Bianca Sep 25 '15 at 19:14
  • @Bianca what do you mean by decorate? – Josh C. Sep 25 '15 at 19:31
  • Add some extra information to the header. – Bianca Sep 25 '15 at 20:24
  • I have a JWT token stored in a cookie, which contains a claim with an xsrf token. I would like to extract the xsrf token and add it to the response header of the first response (I suspect that there is one when the node server is started an the angular app is served) to be able to re-send it to the api on subsequent calls. I got the idea from here: https://auth0.com/blog/2014/01/27/ten-things-you-should-know-about-tokens-and-cookies/ – Bianca Sep 25 '15 at 20:31
0

You can use the AngularJS $http service to make a GET request on an express route, wich gives you some json with the data you are trying to use in your front end.

aFerrer
  • 325
  • 1
  • 9
  • Isn't there a route from which the initial page gets served? I need to decorate all the angular requests with some info coming from the API and for that I need to figure out which is the route from which the first page of the application is served. I was assuming that there is such a route and that I would not need to do a GET request. – Bianca Sep 25 '15 at 19:10
  • Angular has a directive called ng-init. You can use it to pupolate $scope data at the moment that a controller is loaded. You could inject the data in the html on the backend on to the ng-init and then, the frontend is going to have this data in the $scope. – aFerrer Sep 26 '15 at 16:56
0

Use NgInit

<div ng-init="names=[{data: 'a'},{data: 'b'}]"></div>

With that line in the html you will have in the controller the $scope.names object so you can do whathever you want with that.

Put that data in the ng-init directive using one of the templates engines that express has.

aFerrer
  • 325
  • 1
  • 9