I come from traditional back-end Java/Spring
environment to learn Angular 2 framework
, and have hard time to grasp many of the fundamental concepts.
I'm reading the Manning book Angular 2 Development with TypeScript and it says I need HTTP-server
to run my SAP'
s. Why is that?
I thought Angular
runs in client machine. So what is exactly function of the server? If I just open the HTML
, Angular
doesn't do its magic.
In fact, Angular applications suppose to be accessed using the HTTP protocol so it's a good idea to use an HTTP server for development. You will be in the "same conditions". In addition this prevents from having some limitations and restrictions:
file://
protocol and you can some security restrictions according to browsers.See these links for more details about these issues:
Regarding the choice of a Web server, static Web servers are enough for Angular applications and there are several lightweight HTTP servers for this purpose:
http-server
- https://github.com/indexzero/http-serverlite-server
- https://github.com/johnpapa/lite-serverAn interesting feature (with lite-server
for example) consists in the live reload. The server detects when you updated some contents (HTML, JavaScript, CSS) and automatically reloads corresponding pages in the browser. This allows you to be more efficient when implementing your application.
Finally if you implement both client (Angular) and server sides, these parts should be executed on different servers (in development environment it could / should be different in other environments like production). I mean:
Because of these two servers, you must enable CORS (Cross Origin Resource Sharing) to make possible the Angular application to execute AJAX requests on the server application. As a matter of fact, you aren't on the same domain. It's something that must be configured on the server side to return CORS headers when the browser sends an Origin
header in the request. For more details you can have a look at these links:
Another thing to be aware of if you use the routing feature of Angular is that the HTML5 history mode simulates some "real" addresses but you need in this case some configuration on your webserver to make load the index.html (your entry point file) for each URLs corresponding to each routes. See this answer for more details: When I refresh my website I get a 404. This is with Angular2 and firebase.
The last point to consider is the packaging of your application for your production environment. You need to make your application efficient (minifying elements, concating JavaScript files, preloading templates, ...). At this level, it's not necessary to keep two separate servers and the Angular part can be packaged within the server side application. In this case, the latter must be able to serve some statis files.
Hope it helps you, Thierry