Implementation of the WHATWG Fetch API on libp2p streams
http implements the WHATWG Fetch api. It can be used as a drop in replacement for the browser's fetch function. It supports http, https, and multiaddr URIs. Use HTTP in p2p networks.
See the examples/
for full examples of how to use the HTTP service.
import { createLibp2p } from 'libp2p'
import { http } from '@libp2p/http-fetch'
const node = await createLibp2p({
// other options ...
services: {
http: http()
}
})
await node.start()
// Make an http request to a libp2p peer
let resp = await node.services.http.fetch('multiaddr:/dns4/localhost/tcp/1234')
// Or a traditional HTTP request
resp = await node.services.http.fetch('multiaddr:/dns4/example.com/tcp/443/tls/http')
// And of course, you can use the fetch API as you normally would
resp = await node.services.http.fetch('https://example.com')
// This gives you the accessibility of the fetch API with the flexibility of using a p2p network.
$ npm i @libp2p/http-fetch
<script>
tagLoading this module through a script tag will make its exports available as Libp2pHttpFetch
in the global namespace.
<script src="https://unpkg.com/@libp2p/http-fetch/dist/index.min.js"></script>
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.