This module exports several utility functions to make creating HTTP servers that run over HTTP quick and easy.
import { createLibp2p } from 'libp2p'import { http } from '@libp2p/http'import { nodeServer } from '@libp2p/http-server'import { createServer } from 'node:http'const server = createServer((req, res) => { res.write('Hello world!') res.end()})const node = await createLibp2p({ // ...other settings services: { http: http({ server: nodeServer(server) }) }}) Copy
import { createLibp2p } from 'libp2p'import { http } from '@libp2p/http'import { nodeServer } from '@libp2p/http-server'import { createServer } from 'node:http'const server = createServer((req, res) => { res.write('Hello world!') res.end()})const node = await createLibp2p({ // ...other settings services: { http: http({ server: nodeServer(server) }) }})
The previous example can also run in a non-Node.js environment by using the supplied createServer function which implements the same API as Node.js just without depending on any Node.js internals.
createServer
import { createServer } from '@libp2p/http-server/node'const server = createServer((req, res) => { res.write('Hello world!') res.end()}) Copy
import { createServer } from '@libp2p/http-server/node'const server = createServer((req, res) => { res.write('Hello world!') res.end()})
This module exports several utility functions to make creating HTTP servers that run over HTTP quick and easy.
Example: Create a Node.js server
Example: Create a Node.js server in a browser
The previous example can also run in a non-Node.js environment by using the supplied
createServer
function which implements the same API as Node.js just without depending on any Node.js internals.