fix oRPC context, route for express

This commit is contained in:
Aman Varshney
2025-04-17 22:13:00 +05:30
parent b8c16d0610
commit 2a51f85989
4 changed files with 19 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
fix express server orpc route

View File

@@ -70,13 +70,12 @@ export async function createContext({ context }: CreateContextOptions) {
}
{{else if (eq backend 'express')}}
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
{{#if auth}}
import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth";
{{/if}}
export async function createContext(opts: CreateExpressContextOptions) {
export async function createContext(opts: any) {
{{#if auth}}
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers),

View File

@@ -7,6 +7,9 @@ import { appRouter } from "./routers/index";
{{#if (eq api "orpc")}}
import { RPCHandler } from "@orpc/server/node";
import { appRouter } from "./routers";
{{#if auth}}
import { createContext } from "./lib/context";
{{/if}}
{{/if}}
import cors from "cors";
import express from "express";
@@ -16,6 +19,7 @@ import { google } from "@ai-sdk/google";
{{/if}}
{{#if auth}}
import { auth } from "./lib/auth";
import { toNodeHandler } from "better-auth/node";
{{/if}}
const app = express();
@@ -50,7 +54,11 @@ const handler = new RPCHandler(appRouter);
app.use('/rpc{*path}', async (req, res, next) => {
const { matched } = await handler.handle(req, res, {
prefix: '/rpc',
{{#if auth}}
context: await createContext({ req }),
{{else}}
context: {},
{{/if}}
});
if (matched) return;
next();
@@ -58,9 +66,8 @@ app.use('/rpc{*path}', async (req, res, next) => {
{{/if}}
{{#if (includes examples "ai")}}
// AI chat endpoint
app.post("/ai", async (req, res) => {
const { messages = [] } = req.body;
const { messages = [] } = req.body || {};
const result = streamText({
model: google("gemini-1.5-flash"),
messages,
@@ -73,6 +80,7 @@ app.get("/", (_req, res) => {
res.status(200).send("OK");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

View File

@@ -6,7 +6,7 @@ import StackArchitect from "../_components/StackArchitech";
export default function FullScreenStackArchitect() {
return (
<div className="flex h-svh flex-col bg-gradient-to-b from-white to-gray-50 dark:from-gray-950 dark:to-gray-900">
<div className="flex h-svh flex-col overflow-y-auto bg-gradient-to-b from-white to-gray-50 dark:from-gray-950 dark:to-gray-900">
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}