0

On the terminal on my localhost i can see npm say " 404 error /coins"

Then On the page it says Not Found

These URLS work fine

 http://localhost:8808/events/new
 http://localhost:8808/events

But I ADDED in this "coins" one

Go there

http://localhost:8808/coins

Refresh is problem

navbar.component.html

    <li>
      <a [routerLink]="['/coins']">All Coins</a>
    </li>
    <li>
      <a [routerLink]="['/events']">All Events</a>
    </li>
    <li><a [routerLink]="['/events/new']">Create Event</a></li>

Then routes.ts

export const appRoutes:Routes = [
{ path: 'events/new', component: CreateEventComponent },  // process this first , since we have events/:id
{ path: 'events', component: EventsListComponent },
{ path: 'events/:id', component: EventDetailsComponent },
{ path: 'coins', component: CoinsListComponent },
{ path: '', redirectTo: '/events', pathMatch: 'full'}

]

index.html

 <base href="/">

So everything was working until adding this coins... and doing a page refresh , even a hard refresh.

  1. This is not Angular 1 (angularJS ) this is 100% the angular latest 4.0 version with the import { Routes } from '@angular/router'

What could be the problem ?

Update: showing Coin component

import { Component, OnInit } from '@angular/core'
import { CoinService } from './coin.service'

@Component({
    template: `
    <div *ngFor="let coin of coinsList">
     abc
     </div>
    `
})
export class CoinsListComponent implements OnInit {
    coinsList:any[]
    constructor(
        private coinService: CoinService
    ){
    }

    ngOnInit() {
        console.log('oninit coins')
        this.coinsList = this.coinService.getAllCoins()
    }
}
  • Is the server responding with the `index.html` when you make a request to `http://localhost:8808/coins`? If it does not then the angular app will not be loaded when you refresh the page on that route. – Jake Holzinger Aug 10 '17 at 04:12
  • If i click on the Link on the page (spa index.html) i does go to url you said .../coins and console.log does show `oninit coins` which is correct as i have coins-list.componet.ts which has an ngOnIit function with that console.log ... It is when i then right click and refresh the page ... page not found ... so annoying –  Aug 10 '17 at 04:20
  • makes me wonder if this pluralsight angular fundamentals course with that /events redirect is covering up the fact that perhaps 'events' would possibly "blow up" if there wasn't that last redirect ... ughh –  Aug 10 '17 at 04:22
  • can you show your coin component? – brijmcq Aug 10 '17 at 04:26
  • this is weird. your code looks fine. The error should be somewhere else – brijmcq Aug 10 '17 at 04:28
  • showing coin component –  Aug 10 '17 at 04:30
  • it is super strange , i cannot think /see what could be wrong especially when a link is clicked on and it emits code from the component etc –  Aug 10 '17 at 04:31
  • So when you refresh the page on any other route, say `/events` it doesn't give 404 right? – eko Aug 10 '17 at 04:36
  • So if i go to /events and hit refresh I see it briefly showing /events/ then i removes the trailing "/" , with /coins , refresh instantly on refresh the URL stays /coins and it displays the 404 –  Aug 10 '17 at 04:37
  • @echonax Correct , as i typed comment same time as your comment , the /events goes to /events/ then /events i assume the base on the index.html –  Aug 10 '17 at 04:39
  • What do you use in your back-end? – eko Aug 10 '17 at 04:40
  • GET http://localhost:8808/coins 404 (Not Found) , on refresh i see in dev tools console output , but not if i click and navigate with the routerLink on the html nav page of the spa –  Aug 10 '17 at 04:41
  • so on windows, latest version of node/ npm ... i'm not using anything but the npm start –  Aug 10 '17 at 04:42
  • what does your npm start do? what do you use to serve your app? node.js? azure? iis? – eko Aug 10 '17 at 04:43
  • `"start": "tsc && concurrently \"npm run tsc:w\" \"npm run server\" ",` –  Aug 10 '17 at 04:44
  • and then also in package.json `"server": "node node_modules/ng2f-server/server.js",` –  Aug 10 '17 at 04:44
  • So while i have used web api /mvc for years, i have used locally node , iissnode , liteserver and a few other lightweight little servers - once it is deployed i suppose i will end up under IIS -- i just didn't expect this issue –  Aug 10 '17 at 04:46
  • If i change base href to href="." then Url for coins is then `http://localhost:8808/events/coins` not what i want, but refresh works ... –  Aug 10 '17 at 05:06
  • Is your CoinsListComponent imported and declared in your Module? – Stephen R. Smith Aug 14 '17 at 04:56
  • @StephenR.Smith yes –  Aug 14 '17 at 05:48
  • 1
    404 is definitely coming from your server, most likely your server is not redirecting all the calls to `index.html`. If there is a 404, means Angular app hasn't started, – Milad Aug 14 '17 at 09:12
  • Trying making the href to be absolute path, and making sure your server configuration is correctly redirecting, – Milad Aug 14 '17 at 09:14
  • are you able to reproduce this issue if you using _ng serve_ and visiting _localhost:4200_ ? – Sagar V Aug 14 '17 at 12:46
  • you have to make sure that server routes are properly configured to make this work as you are not using `HashLocationStrategy`. – micronyks Aug 14 '17 at 16:27
  • @micronyks can you explain in an answer –  Aug 15 '17 at 05:52
  • what do you want to understand @JeremyMiller ? – micronyks Aug 15 '17 at 06:08

3 Answers3

0

What are you using server side? ExpressJS? If so seems that you haven't setup something like

server/server.ts

const app: Application = express();

app.route('/api/coins/').get(coinsRoute);

server/coinsRoute.ts

export function coinRoute(req, res) {

res.status(200).json({ ...
   });
}

And you have a tab running the Express server as well as the app in another tab in your console.

JGFMK
  • 8,425
  • 4
  • 58
  • 92
0

Use useHash:true withing @NgModule in app.module.ts

RouterModule.forRoot(appRoutes, { useHash: true })
S.I.
  • 3,250
  • 12
  • 48
  • 77
sanjay kumar
  • 838
  • 6
  • 10
0

Keep your app running with a node server. I fixed with the following code.

//server.js

Keep your app running with a node server. I fixed with the following code.

//server.js

   
    'use strict';
    
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var logger = require('morgan');
    var port = process.env.PORT || 8000;
    var path = require('path');
    
    
    var environment = process.env.NODE_ENV;
    
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    app.use(logger('dev'));
    
    // var filespath = require("path").join(__dirname, "index.html");
    // console.log(filespath);
    
    switch (environment) {
      case 'build':
        console.log('environment')
        app.use(express.static(path.join(__dirname, "/")));
    
      // Any deep link calls should return index.html
      app.use('/*', express.static(path.join(__dirname, "index.html")));
        break;
      default:
        console.log('development');
        app.use(express.static(path.join(__dirname, "/")));
    
      // Any deep link calls should return index.html
      app.use('/*', express.static(path.join(__dirname, "index.html")));
        break;
    }
    
    
    app.listen(port, function() {
      console.log('Express server listening on port ' + port);
      console.log('env = ' + app.get('env') +
        '\n__dirname = ' + __dirname +
        '\nprocess.cwd = ' + process.cwd());
    });

for more you can see my code in git: https://github.com/HelloSofts/hoq-prod

for more you can see my code in git: https://github.com/HelloSofts/hoq-prod

Zahirul Haque
  • 1,599
  • 17
  • 22