1

I have an Angular app working properly at localhost:4200 by using ng serve; The node server is at localhost:3000.

When i do ng build a bundle file is being created and served properly at localhost:3000 because of app.use(express.static(path.join(__dirname, 'path/to/dist'))); but the routes which worked at localhost:4200/user/id is giving an error at localhost:3000/user/id stating it Cannot GET /user/id.

Any ideas what is causing the problem? I have included <base href="/"> in the index.html file.

Tristan Hessell
  • 930
  • 10
  • 21
Nithin
  • 1,387
  • 4
  • 19
  • 48
  • 1
    what Routing Location Strategy are you using? Just double checking whether you did not miss "/#/" as in localhost:3000/#/user/id – Morphing Coffee Nov 19 '17 at 04:19
  • Hey, The routes work at ```localhost:3000/#/user/id```, But i'm not understanding what is happening here. – Nithin Nov 19 '17 at 04:25
  • 1
    Because you're not using history mode but hashmode https://stackoverflow.com/a/41662473/2880747 – WilomGfx Nov 19 '17 at 04:28

1 Answers1

2

Sounds like your Routing Location Strategy is causing the issue.

Notes straight from Google:
Angular 4 documentation

URL depending on your chosen strategy:
PathLocationStrategy: localhost:3000/user/id
HashLocationStrategy: localhost:3000/#/user/id

You can modify useHash property in your app module to switch between the two:

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot(routes, { useHash: true })  // .../#/user/id
  ], ...
});

That said, if you mismatch the URLs, your GET should fail.

Morphing Coffee
  • 815
  • 1
  • 10
  • 20
  • Yep, this works but how to use ```PathLocationStrategy``` as showing users as ```http://localhost:3000/index.html#/user/id``` is not friendly. – Nithin Nov 19 '17 at 04:50
  • In your project find "RouterModule.forRoot" call. Modify it to "RouterModule.forRoot(routes, { useHash: false })" . Confirm whether you can then access "localhost:3000/user/id" or not – Morphing Coffee Nov 19 '17 at 05:00
  • No, i'm not able to access it after making `useHash: false` – Nithin Nov 19 '17 at 06:06