Skip to content

Commit

Permalink
Fix example code
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jun 7, 2024
1 parent 270a7d7 commit 58b6fca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,28 @@ TODO (need to publish this)

See the `examples/` for full examples of how to use the HTTP service.
```typescript
const node = await createLibp2p({
// other options ...
services: {
http: http()
}
})

await node.start()

// Make an http request to a libp2p peer
const resp = await node.services.http.fetch('multiaddr:/dns4/localhost/tcp/1234')
// Or a traditional HTTP request
const 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
const resp = await node.services.http.fetch('https://example.com')

// This gives you the accessiblity of the fetch API with the flexibility of using a p2p network.
import { createLibp2p } from 'libp2p'
import { http } from '../dist/src/index.js'

async function main () {
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 accessiblity of the fetch API with the flexibility of using a p2p network.
}

main()
```
22 changes: 14 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@
*
* See the `examples/` for full examples of how to use the HTTP service.
* ```typescript
* import { createLibp2p } from 'libp2p'
* import { http } from '../dist/src/index.js'
* async function main () {
* const node = await createLibp2p({
* // other options ...
* services: {
* http: http()
* }
* })
* }
*
* })
* await node.start()
*
* // Make an http request to a libp2p peer
* const resp = await node.services.http.fetch('multiaddr:/dns4/localhost/tcp/1234')
* let resp = await node.services.http.fetch('multiaddr:/dns4/localhost/tcp/1234')
* // Or a traditional HTTP request
* const resp = await node.services.http.fetch('multiaddr:/dns4/example.com/tcp/443/tls/http')
* 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
* const resp = await node.services.http.fetch('https://example.com')
*
* resp = await node.services.http.fetch('https://example.com')
* // This gives you the accessiblity of the fetch API with the flexibility of using a p2p network.
* }
* main()
* ```
*/

Expand Down

0 comments on commit 58b6fca

Please sign in to comment.