Quiero leer una respuesta de una solicitud get http, mi servidor está en Javascript y la parte donde envío una respuesta es:
app.get('/getReport',function(req,res) { try { const data=fs.readFileSync('./report.txt', 'utf8') res.end(data) } catch (err) { res.end("File not found") } })Si uso Postman, puedo ver el contenido del archivo del informe. Quiero leer este texto en mi front-end angular, así que creo esta función en mi home.service.ts:
public getReport(){ return this.http.get<any>(this.url+'/getReport').subscribe(data => { this.value = data;}) }no funciona ¿Qué puedo hacer para leer mi contenido correctamente?
Verifique su configuración . Tienes que importar algunos módulos antes de recibir solicitudes http:
@NgModule({ imports: [ BrowserModule, // import HttpClientModule after BrowserModule. HttpClientModule, ],...Para recibir cualquier respuesta, debe iniciar una comunicación con su API/servidor suscribiéndose al método 'getReport' .
¿COMO PUEDES HACER ESO?
Tienes ese método en tu home.service.ts .
// 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, ... ) {} constructor( ... private homeService: HomeService, ... ) { this.homeService.getReport .subscribe((_response) => { console.log(_response); this.report = _response; alert(this.report); }); }