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.
You don't need a server-side engine to dynamically compose application pages because Angular does that on the client-side. If the application uses the Angular router, you must configure the server to return the application's host page ( index.html ) when asked for a file that it does not have.
All of the angular code, from the patial html, to the controllers are executed on the client. The servers job is to send these files up to the client when they are needed. You can tell that the angular controllers are run on the client because you used a script tag to load them in the index. html file.
A simple dev-server designed for Single Page App (SPA) developers. angular-http-server is not, and makes no claims to be, a production server. It returns a file if it exists (ex. your-icon. png, index.
Firebase hosting Firebase hosting is the best hosting to use to deploy your angular app or react app.
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-server
lite-server
- https://github.com/johnpapa/lite-server
An 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With