Angular 12 web version displays error message: "404 not found" for the requested URL

I recently completed my first website using Angular and uploaded it to the server successfully. When browsing through the pages, everything seems fine. However, I encountered an issue when trying to access specific URLs by copying and pasting them into the browser. For instance, if I paste the URL 'www.project.com/app/home/users/22' directly, I receive a '404 not found' error. But if I navigate to the 'user 22' link from the homepage URL 'www.project.com/', it works perfectly. Can anyone explain why this is happening and how I can resolve it?

Thank you

app-routing.module.ts


    import { NgModule } from '@angular/core';
    import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
    import { AuthGuard } from "./guards/auth.guard";
    import { GuestPageModule } from "./pages/guest/guest.module";

    // Define route paths
    const routes: Routes = [
      // Redirect to welcome page
      { path: '', redirectTo: 'welcome', pathMatch: 'full'},
      // Load the welcome module with authentication guard
      { path: 'welcome', loadChildren: () => import('./pages/welcome/welcome.module').then(m => m.WelcomePageModule) },
      
      // Other route definitions...
    
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
  

The URL I'm trying to reach is

Link example:

<a (click)="goUser(user)">
    
    // Function to handle user navigation
    goUser(user: any) {
      user = user;
      this.userData.userViewed(user).pipe(
        map((data: any) => {
          if (data.success) {
            const navigationExtras: NavigationExtras = {
              state: {
                user: user
              }
            };
            // Navigate to the user details page
            this.router.navigate(['guest/user' + id], navigationExtras);
          }
        })
      ).subscribe()
    }
  

Guest routing module:


    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';

    import { GuestPage } from './guest.page';

    const routes: Routes = [
      // Basic route configuration for guest page
      
      // Other nested routes...
    
    ];

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

Answer №1

It appears that there may be an issue with the lazy loading implementation in the guest module.

  {
    path: 'guest',
    loadChildren: () => import('./pages/guest/guest.module').then(m => m.GuestPageModule)
  }

For more information, please refer to the documentation at: https://angular.io/guide/lazy-loading-ngmodules#create-a-feature-module-with-routing

Additionally, it seems unusual that there is no leading slash / before the id - double check if the slash is included in the id variable.

this.router.navigate(['guest/user' + id], navigationExtras);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process of adding an m4v video to a create-next-app using typescript?

I encountered an issue with the following error: ./components/Hero.tsx:2:0 Module not found: Can't resolve '../media/HeroVideo1-Red-Compressed.m4v' 1 | import React, { useState } from 'react'; > 2 | import Video from '../ ...

Angular - Automatically update array list once a new object is added

Currently, I'm exploring ways to automatically update the ngFor list when a new object is added to the array. Here's what I have so far: component.html export class HomePage implements OnInit { collections: Collection[]; public show = t ...

Tips for adding items to a Form Array in Angular

I am working on a project with dynamic checkboxes that retrieve data from an API. Below is the HTML file: <form [formGroup]="form" (ngSubmit)="submit()"> <label formArrayName="summons" *ngFor="let order of form.controls.summons.controls; let i ...

how to implement dynamic water fill effects using SVG in an Angular application

Take a look at the code snippet here HTML TypeScript data = [ { name: 'server1', humidity: '50.9' }, { name: 'server2', humidity: '52.9', }, { name: 'server3', humidity: ...

Are my Angular CLI animations not working due to a version compatibility issue?

I've been working on a project that had Angular set up when I started. However, the animations are not functioning correctly. The mat input placeholder doesn't disappear when typing, and the mat-select drop-down is not working. Here is my packag ...

How do I rearrange the order of a collection in Firestore using a reference that I already have?

Is there a method to maintain a reference of the same collection while altering the ordering using firestore? TLDR: I am looking to achieve similar functionality as demonstrated in: , but since my data is sourced from firestore, I am struggling to find th ...

Converting an Array of Objects into a single Object in React: A Tutorial

AccessData fetching information from the database using graphql { id: '', name: '', regions: [ { id: '', name: '', districts: [ { id: '', ...

Index.js is dynamically importing a .tsx file post-build, with a specific focus on Windows

While working on my project, I decided to customize a module by cloning it and making some changes. After installing the dependencies and building it, I encountered an error when trying to run it. The error message stated: Error: Unable to resolve module & ...

Tips for avoiding the reconstruction of components in if-else conditions within Angular

After just joining the Angular4 club as a ReactJS developer, I find myself using basic conditional statements with a very simple condition. Take a look: <div class="app-component"> <app-country *ngIf="condition"></app-country> ...

PhpStorm is unable to resolve the @ionic/angular module

I have encountered a peculiar issue with my Ionic v4 project. While the project runs smoothly, PhpStorm seems unable to locate my references to @ionic. https://i.stack.imgur.com/umFnj.png Interestingly, upon inspecting the code, I realized that it is act ...

What is the best way to ensure the secure signing of a transaction in my Solana decentralized application (

I am currently involved in an NFT project that recently experienced a security breach, and I am developing a dapp to rectify the situation. Our plan is to eliminate all NFTs from the compromised collection and issue a new set of NFTs using our updated auth ...

Encountered an issue while trying to open the appointment editor in Angular Syncfusion Schedule - Getting an error message saying "Cannot read property '0

Utilizing the Syncfusion Angular schedule and looking to add a template for header quick information. https://i.stack.imgur.com/KI1Ie.png <ng-template #quickInfoTemplatesHeader let-data> <div *ngIf="data.elementTy ...

What is the best way to create a React text box that exclusively accepts numeric values or remains empty, and automatically displays the number keypad on mobile devices?

While there are numerous similar questions on StackOverflow, none of them fully address all of my requirements in a single solution. Any assistance would be greatly appreciated. The Issue at Hand Within my React application, I am in need of a text box tha ...

Using DevExtreme Angular to establish an initial filter value for a DxDataGrid custom headerFilter

What is the best way to set a starting headerFilter value with a custom expression like this? Using filterValues="xxx > 0" does not seem to work. <dxi-column caption="" dataField="xxx" [headerFilter]="headerFilter ...

What is the best way to filter by enum value in Typescript?

If I define an enum as follows: export enum Status { InProgress = 0, Completed = 1, Cancelled = 2 } and have a class that references the enum: import { Status } from "./Status"; export class TaskDto { public name: string = null; public c ...

Angular - ngx-Pagination malfunctioning in an unexpected manner

Currently in my Angular project, I am setting up server-side pagination using ngx-bootstrap-pagination with ASP.NET Core-6 as the backend. This is the service implementation: getPaymentStatus(): Observable<IPaymentStatus[]> { return this.http.get& ...

Displaying data issue with Typescript typed array

While working with Angular 7 and Typescript 3, I encountered an issue where I was trying to pre-populate an array of ingredients in a service for use in a component. However, when I checked the console output, the array was showing empty objects. If I ini ...

Error in Angular 2 component when loading background images using relative URLs from an external CSS skin

In my angular2 component, I am utilizing a third-party JavaScript library. The skin CSS of the component attempts to load images using relative URL paths. Since I am following a component-based architecture, I prefer to have all component dependencies enca ...

Having trouble getting the ionic lib update command line to work properly?

My current situation: $ionic lib update you sure you want to replace D:\Projects\cda-volunteer\www\lib\ionic with an updated version of Ionic? (yes/no): yes Unable to receive version data Here's my ionic info: Cordova ...

Two separate menus for each root file in Ionic 2

In my Ionic 2 project, I have developed two root files: app.component and report-menu. In order to make the report-menu module accessible in the app.component.module.ts file, I imported it successfully. However, I am facing an issue where one menu is dis ...