1

I have uploaded my project to Heroku which have Django Rest as backend and Angular as frontend. Everything is working fine locally except I am unable to get API request (branches/) in https://branches-front-shiv.herokuapp.com/.

enter image description here

So in above picture as you can see there is blank output in the left side which is from branches-front-shiv.herokuapp.com and in right side we have table, pagination controls. I don't have any errors it's just a blank page (because of these API request I guess). I don't know how to solve it.

components.ts

export class ShowBranchesComponent implements OnInit {

  branches: any = [];
  branchName: any; // any
  p: number = 1;

  options = ["All Cities", 'MUMBAI', 'KOLKATA', 'DEHLI', 'CHANDIGARH', 'NOIDA']
  selected: any = "All Cities";
  selectedData: any;

  constructor(private service: ShareService) { }

  ngOnInit(): void {
    // this.refreshBranchList();
    this.service.getBranchList().subscribe((response: any) => {
      this.branches = response;
      this.selectedData = this.branches;
    });
  }

service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})

export class ShareService {
  readonly APIUrl = "https://branches-shiv.herokuapp.com";
  readonly PhotoUrl = "http://127.0.0.1:8000/media/";

  constructor(private http: HttpClient) { }

  getBranchList(): Observable<any[]> {
    return this.http.get<any[]>(this.APIUrl + '/branches/');
  }

  getAllNames(): Observable<any[]> {
    return this.http.get<any[]>(this.APIUrl + '/branches/');
  }
}

App.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BranchesComponent } from './branches/branches.component';
import { ShowBranchesComponent } from './branches/show-branches/show-branches.component';
import { ShareService } from './share.service'

import { HttpClientModule } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Ng2SearchPipeModule } from 'ng2-search-filter';
import { Ng2OrderModule } from 'ng2-order-pipe';
import { NgxPaginationModule } from 'ngx-pagination';
import { NgMultiSelectDropDownModule } from 'ng-multiselect-dropdown';

@NgModule({
  declarations: [
    AppComponent,
    BranchesComponent,
    ShowBranchesComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    Ng2SearchPipeModule,
    Ng2OrderModule,
    NgxPaginationModule,
    NgMultiSelectDropDownModule.forRoot()
  ],
  providers: [ShareService],
  bootstrap: [AppComponent]
})

mention any other code file you want to see in comment section. Thankyou.

app.components.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'angular';
}

server.js

const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(__dirname + '/dist/angular'));
app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname +
        '/dist/myapp/index.html'));
});
app.listen(process.env.PORT || 8080);

app.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { BranchesComponent } from './branches/branches.component';

const routes: Routes = [
  { path: 'branches', component: BranchesComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
6
  • share app.component.html or the html file where you have put <router-outlet></router-outlet> Commented Jan 8, 2021 at 16:28
  • in localhost you are showing /branches/ and in Heroku you are on / path Commented Jan 8, 2021 at 16:29
  • but after going to /branches/ it is saying not Found which is obvious because I don't have any '/branches/' API request for some reasons. Commented Jan 8, 2021 at 17:02
  • 1
    I think your app is totally fine, you just have to add a router link to /branches/ in app.component.html Commented Jan 8, 2021 at 18:20
  • please explain in code. Commented Jan 9, 2021 at 13:03

2 Answers 2

2

In your app.routing.ts you are defining branches as urls path,

const routes: Routes = [
  { path: 'branches', component: BranchesComponent },
];

service.ts there is also reference /branches/ for getting API request, which might over writing your urls paths.

Try changing one of them to empty string:

const routes: Routes = [
      { path: '', component: BranchesComponent },
    ];
Sign up to request clarification or add additional context in comments.

Comments

0

You need to define rules to rewrite URL and point to index.html in your reverse proxy server. Angular is a SPA with its own router. You need to redirect all requests to index.html which will deal with the routing.

5 Comments

Any example would be a grate help.
Depends on your reverse proxy. Is it nginx or apache?
I'm not using these technologies. It was just a simple project so. Anyway other I can get those request. I mean it is working fine in local host the only problem with Heroku server when it online.
I need more details on how you did setup the app on Heroku. How is it served? What does the routing look like?
I added a routing file from my code please take a look it is in end of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.