I wanted to render the static files of Angular from my node server.
As an experiment, Below are the steps i took:
Created a dist folder by building my Angular project using
ng build --prod
Copied the dist folder to the root directory of my node server
Configured express app in my node server to point to index.html of dist folder. Below are the snips of configuration code i wrote -
app.use(express.static(path.join(__dirname, '../front-end/dist/')))
app.get('*', (req, res) => {
return res.sendFile(path.join(__dirname, '../front-end/dist/my-first-app/index.html'))
})
When i run my node server locally on port 3000 If i am correct, it should get redirected to index.html page within the compiled angular dist folder. Well, i see the redirection attempt being made by node. However, after redirection the browser is failing to load the html within the index.html file. It throws the below error on the console-
Uncaught SyntaxError: Unexpected token <
The above error tells clearly that the browser failed to load the html page. I believe my understanding is correct. But i am not able to figure out why this error gets logged. I hope i get some answers here.
I am trying to work without Angular CLI's ng serve command. If i succeed, i will try to attempt this on cloud. My intension is i want to make use of only static files generated out of Angular build rather than loading them using ng serve and also to render these files on browser from a call using node. Since back-end server has to be the only running server and frontend should not be a server but rather simple static files which needs to be served with data from the former. I hope i made sense.
app.use(express.static(path.join(__dirname, '../front-end/dist/my-first-app')))So basically, i had missed to specifymy-first-appwhich had all the build files