1
votes

I have an angular 4 app i am serving from node js

My server structure is as follows

.env
server.js
/admin               //angular 4 app directory
    index.html
    ....other angular files and assets folder
/modules
/config
...other files and folders at root level

server.js is my server file and i am attempting to serve the angular 4 app with the following code.

app.get('/admin', (req, res) => {
      res.sendFile(path.join(__dirname, 'admin/index.html'));
});

So i should get the angular app at http://localhost:3020/admin

When i got to http://localhost:3020/admin i get the following error in the console

SyntaxError: expected expression, got '<'[Learn More]   
                                                    inline.a808723d583c934ba8bf.bundle.js:1

SyntaxError: expected expression, got '<'[Learn More]
                                                    polyfills.5d1d5b016f19755a7d41.bundle.js:1

SyntaxError: expected expression, got '<'[Learn More]
                                                    main.036acd83ead6248588f0.bundle.js:1

When i click on any of the above links it indicates that the href of my angular app is

I noted that and i set it using https://angular.io/guide/deployment#!#fallback with the following command ng build --prod --base-href= "/admin/" and also ng build --prod --base-href= /admin/. but i keep getting the same error as above.

In my index.html the href is set to /admin/ i keep getting the same errors.

I have visited the following links but did not find anything about the error message i am getting.

https://github.com/angular/angular-cli/issues/4570

https://github.com/angular/angular-cli/issues/1080

1
Are you using ng serve to serve your website? - Jonathan Lam
No i'm not using ng serve.... - Joshua Majebi
Can you try ng build --prod -op=./admin/ to build to the correct directory? - Jonathan Lam
let me try that pls - Joshua Majebi
That worked....problem is i am not developing in the admin folder. I was just transferring the ng build output files to the admin folder after the build completed.....do you recommend me developing and building in the admin folder? - Joshua Majebi

1 Answers

1
votes

Try using the command to build the Angular output to the correct directory:

ng build --prod -op=./path/to/admin/

To answer your comment, I'd suggest working with the angular source in a different directory to avoid clutter, and only have the built output in the admin folder.