Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
How can I recive a response data from an http request in Angular?

I want to read a response from a get http request, my server is in Javascript and the part of the where I send a response is:

app.get('/getReport',function(req,res) {
  try {
    const data=fs.readFileSync('./report.txt', 'utf8')
    res.end(data)
  }
  catch (err) {
    res.end("File not found")
  }
})

If if I use Postman I can see the content of the report file. I want to read this text on my Angular front-end so I create this function in my home.service.ts:

   public getReport(){
   return this.http.get<any>(this.url+'/getReport').subscribe(data => {
    this.value = data;})        
  }

It doesn't work. what can I do to read my content correctly?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Check your setup. You have to import some modules before get http requests:

@NgModule({
  imports: [
    BrowserModule,
    // import HttpClientModule after BrowserModule.
    HttpClientModule,
  ],...
about 4 years ago · Juan Pablo Isaza Report

0

In order to recive any response, you have to start a communication with your API/server subscribing to the 'getReport' method.

HOW CAN YOU DO THAT?

You have that method in your home.service.ts.

  1. Inject that service in the component where you want to load your report (i.e 'home-component'):
// NOTE: Fit all the paths to your specific system
...
import { HomeService } from '../services/home.service';
...

@Component({
  selector: 'app-home-component',
  templateUrl: './home-component.html',
  styleUrls: ['./home-component.scss'],
})
export class HomeComponent {

public report;

 constructor(
...
    private homeService: HomeService,
...

  ) {}
  1. Subscribe to the observable 'getReport' method of homeService, and get the desired data. For instance, change the constructor like this:
 constructor(
...
    private homeService: HomeService,
...

  ) {

       this.homeService.getReport
        .subscribe((_response) => {
           console.log(_response);
           this.report = _response;
           alert(this.report);
       });

}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!