Skip to content

Express.js Adapter

Express.js is a popular Node.js framework for building web applications. For additional context, refer to the HTTP Adapter guide.

WARNING

Express's body-parser handles common request body types, and oRPC will use the parsed body if available. However, it doesn't support features like Bracket Notation, and in case you upload a file with application/json, it may be parsed as plain JSON instead of a File. To avoid these issues, register any body-parsing middleware after your oRPC middleware or only on routes that don't use oRPC.

Basic

ts
import express from 'express'
import cors from 'cors'
import { RPCHandler } from '@orpc/server/node'

const app = express()

app.use(cors())

const handler = new RPCHandler(router)

app.use('/rpc{/*path}', async (req, res, next) => {
  const { matched } = await handler.handle(req, res, {
    prefix: '/rpc',
    context: {},
  })

  if (matched) {
    return
  }

  next()
})

app.listen(3000, () => console.log('Server listening on port 3000'))

INFO

The handler can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or another custom handler.

Released under the MIT License.