Before asking this question, I read all the similar questions in StackOverflow and tried all the solutions mentioned by other users but still stuck in this seemingly simple problem. Everything works fine in my local MacBook Air M1 while developing but after producing the production build it is not working in my Windows Server 2019 Standard which is my production server. The following is the simple version of my source code.
AppModule:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule
],
bootstrap: [AppComponent]
})
export class AppModule {}
AppRoutingModule:
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
const routes: Routes = [
{ path: "", pathMatch: "full", redirectTo: "/test" },
{
path: "test",
loadChildren: () =>
import("@project-name/test").then((m) => m.TestModule),
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
app.component.html:
<h1>I am app component!</h1>
<router-outlet></router-outlet>
TestModule:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { TestComponent } from './test.component';
@NgModule({
imports: [CommonModule, RouterModule.forChild([{ path: '', component: TestComponent }])],
declarations: [TestComponent]
})
export class TestModule {}
TestComponent:
<h1>I am a test component!</h1>
In production server, when I hit the localhost/my-project I will the test component successfully loaded; but when I hit the localhost/my-project/test I see 404 Not Found and nothing is shown in the browser. I should mention that I have hosted the built app on nginx/1.19.10 and I am developing the app using Nx monorepo.
What I have tried so far but the problem not solved yet:
esnextng build --prod --build-optimizer=false --aot=false command and configuration section in the angular.json