0

I want to use http2 for client side in my angular project.

const http2 = require("http2");
const client = http2.connect("http://localhost:8443");

const req = client.request({
    ":path": "/"
});

When I wrote this code block for http2 request, I get error Module not found: Error: Can't resolve 'http2' in '/path' .

There was http2 package ("npm install http2" command), but this package showing "This package has been deprecated" and "Use the built-in module in node 9.0.0 or newer, instead". Thus, I cannot use this package.

Thus, I cannot get data from server using http2 client like nodejs. How can I fix these problem?

  • Node version: v13.3.0
  • npm version: 6.13.1
  • @angular/cli: "~7.3.7"
Arya
  • 43
  • 1
  • 6

1 Answers1

1

You need to use the built in HttpClient to get the data from server.

1- Make service and import httpclient in your service like below.

import { HttpClient, HttpHeaders } from '@angular/common/http';

getData() {
    return this.http.get(serverUrl, {
        headers: new HttpHeaders({
          'Content-Type': 'application/json'
        })
    });
}

2- import service in your component. Dependency Injection //fetcher

import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}

3- subscribe service observable to you component.

saveUpdatedGeojson() {
    this.fetcherService.getData().subscribe((response: any) => {
       console.log(response)
    }, error => {
       console.log(error);
    })
}
Naseer
  • 184
  • 1
  • 9
  • Thank you for answering. I have another question. When I write the code as above, if the server part accepts only http2, will there be a response return? Or should the server part take http and convert it to http2? – Arya Jan 14 '20 at 07:04
  • This might be helpful [http1.1 and http2](https://stackoverflow.com/questions/36500050/what-if-an-http-1-1-client-talk-to-an-http-2-only-server-and-what-if-an-http-2-c) – Naseer Jan 14 '20 at 14:52
  • is the above link helpfull? – Naseer Jan 15 '20 at 05:20