Express.js Adapter
Use oRPC inside an Express.js project by mounting a handler as a middleware.
Express.js is a popular Node.js framework for building web applications. oRPC integrates with it through the Node HTTP Adapter.
Basic
import { onError } from '@orpc/server'
import { RPCHandler } from '@orpc/server/node'
import cors from 'cors'
import express from 'express'
const app = express()
app.use(cors())
const handler = new RPCHandler(router, {
interceptors: [
onError((error) => {
console.error(error)
}),
],
})
app.use('/rpc{/*path}', async (req, res, next) => {
const { matched } = await handler.handle(req, res, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
if (matched) {
return
}
next()
})
app.listen(3000, () => console.log('Server listening on port 3000'))