Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda