Skip to content
Advertisement

How to remove # from URL and 404 reload issue in angular build and nodejs application?

I have Angular9 and nodejs application. I am doing ng build --prod for production build and putting that build file inside nodejs public folder and now i am able to access the page perfectly.

but my problem is in URL i want to remove #. Currentlly i am getting as

http://localhost:8080/#/about

http://localhost:8080/#/admin/create/blog

I user some technique to over come this like i use

  1. {useHash: false}
  2. PathLocationStrategy : using this i did angular build and put that build folder in Nodejs Public folder. And in url # is not showing. But when i tried to reload that it is showing 404 not found error.

I know there will be some extra things need to be added in Nodejs code. But i am not sure how to do this. I check lots of solutions but not able to fix this

I want URL not to append # and if i refresh it should not throw any 404 error. can anyone guide me how to resolve this?

Project structure:

enter image description here

NodeJs code index.js

JavaScript

Advertisement

Answer

I found one npm package, which is using browser history to load the pages. which is fixing my above issue.

Npm Package: https://www.npmjs.com/package/connect-history-api-fallback

What i followed in my code:

Angular App:

  1. Removed HashLocationStrategy, LocationStrategy and { useHash: true } from my code. I mean not adding any code which is use for resolving reload issue and which was appending # in URL. I removed this related code.
  2. build: ng build –prod

  3. copy the dist folder to nodejs public folder

Node and Express Js project change:

i just changed in index.js file.

  1. install above npm package : npm i connect-history-api-fallback --save
  2. Did code change in index.js. below code i have added in index.js

What i changed in Index.js

JavaScript

Index.js full code

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement