Casper Nybroe
Casper Nybroe

Reputation: 1199

Trying to reach Angular Universal (SSR) route gives 404 (Cannot Get)

I have created an SSR Angular app which I like to reach from an api or postman. I need the html generated from the SSR app.

The routings for the app is as follows:

const routes: Routes = [
{
  path: 'test',
  component: TestComponent
}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

If I navigate to http://localhost:4200/test in Chrome I am succesfully routet to TestComponent and the content is shown

If I enter the root of the angular app (http://localhost:4200) in Postman I get a 200 and the html is shown as a response.

But if I enter the test route in postman (http://localhost:4200/test) I get a 404 and the content in the response is:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot GET /test</pre>
</body>

</html>

What am I missing here? I haven't configured any proxy and I am not trying to hit an api route either.

Upvotes: 0

Views: 598

Answers (1)

David
David

Reputation: 34445

4200 is the port for client side development.

If you want to test that SSR works correctly, you need to use the port specified in your server.ts file, which I think is 4000 by default

http://localhost:4000/test

Note: I'm not sure why, but I found that to access the a specific route on client app (port 4200) using Postman you need to add the Accept header in Postman.

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3

With that header set, you should be able to reach http://localhost:4200/test using Postman, but once again you will not be testing SSR with that port

Upvotes: 2

Related Questions