I am going to develop a simple Angular 2 application. I have created a project with routing, using Angular CLI and added several components to the app using 'ng generate component ' command. Then I specified routing in the app-routing.module.ts as following.
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { UserComponent } from './user/user.component'; import { ErrorComponent } from './error/error.component'; import { SpecialpageComponent } from './specialpage/specialpage.component'; const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'about', component: AboutComponent }, { path: 'user', component: UserComponent }, { path: 'specialpage', component: SpecialpageComponent }, { path: '**', component: ErrorComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule], providers: [] }) export class AppRoutingModule { }
app.module.ts is as following.
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { ErrorComponent } from './error/error.component'; import { UserComponent } from './user/user.component'; import { SpecialpageComponent } from './specialpage/specialpage.component'; @NgModule({ declarations: [ AppComponent, HomeComponent, AboutComponent, ErrorComponent, UserComponent, SpecialpageComponent ], imports: [ BrowserModule, FormsModule, HttpModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
I have not added any modifications for the other components. Then I deployed the application using 'ng serve' command and the app works fine with the links. Eg: http://localhost:4200/about
But when I deploy the project in http-server, the links do not work as expected. I deployed the app using 'http-server ./dist' command and the app gets deployed fine, but the links do not work. When I go to 'http://localhost:4200/about', it gives 404 error.
Am I doing anything wrong? Why 'ng-serve' works and 'http-server' does not work?
Any help would be appreciated. Thanks in advance.
I have uploaded my project to github.
imports: [RouterModule.forRoot(routes, {useHash: true})],
. If it works this way, you need to enable HTML5 pushState on your production server. – SubmissivepathMatch: 'full'
to this routepath: '',
– Submissive127.0.0.1:8081/about
withuseHash: true
or127.0.0.1:8081/#/about
(or whatever the URL looks like exactly after you navigate to the about page)? – SubmissiveuseHash:true
:D. As I said in my initial comment. You have to configure the server to support HTML5 pushState.useHash:true
was only to debug the cause of the issue. – Submissiveindex.html
for any request that points to a non-existing resource. I have seen client-only solutions but never had a closer look and don't know about disadvantages or if this really works. – Submissiveng build
before trying all of this? – Malebranche