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?
CodePudding user response:
Check your setup. You have to import some modules before get http requests:
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],...
CodePudding user response:
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.
First, you have to inject that service in the component where you want to load your report (i.e 'your-component'):
// NOTE: Fit all the paths to your specific system
...
import { HomeService } from '../services/home.service';
...
@Component({
selector: 'app-your-component',
templateUrl: './your-component.html',
styleUrls: ['./your-component.scss'],
})
export class YourComponent implements OnInit {
public report;
constructor(
...
private homeService: HomeService,
...
) {
this.homeService.getReport
.subscribe((_report) => {this.report = _report;}
)
}
