2

I have question that came to mind about an Angular project for which I cannot find a satisfying answer. When I googled for it I only came up with how to deploy the application and not an answer which answered my specific question.

Normally when an angular application is developed we can run ng serve in order to get a development server. When we then visit the dev server we get served the following files which are required for the SPA.

Angular Server SPA

The development server serves our only static HTML asset the index.html file and the necessary bundles of compiled angular code on which our application runs.

When we deploy an Angular application is the Angular application still in need of a server on which serves the application? Or can we just serve this bundle of files from our backend?

5
  • You just need your generated files. Place them in at the root of your web server and it should work perfectly. Commented Apr 18, 2018 at 17:21
  • And with generated files you are referring to the files in the picture of the question? Commented Apr 18, 2018 at 17:22
  • Yes, usually they are in a folder named build or dist Commented Apr 18, 2018 at 17:23
  • Okay so if I understand it correctly: in a nodeJS express app I serve these files on a GET request to the root URL Commented Apr 18, 2018 at 17:25
  • Actually , if you place them at the root of your web server ( var/www/ if you use linux and apache ) it will automaticly hook to the index.html and the index.html will load the rest. Commented Apr 18, 2018 at 17:27

1 Answer 1

2

You need to generate your application files with $ ng build command. The build artifacts will be stored in the dist/ directory. Then you can use a service like Surge as a static web publishing or other webserver.
On the other hand, if your application use Angular Router, what I said above will not work wonderfully. You need to add a middleware.
If you're using Express for example you need to add something like this:

// server.js
const path = require('path');
app.get('/*all', function(req, res) {
  res.sendFile(path.join(__dirname + '/dist/index.html'));
});

Hope I help!

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

2 Comments

Okay so if I send this index file it will automatically include the scripts (as script tags) which enable it to run right?
Yes, all resources and scripts will be copied over automatically as part of a build.

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.