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')}} {{else if (eq backend 'express')}}
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
{{#if auth}} {{#if auth}}
import { fromNodeHeaders } from "better-auth/node"; import { fromNodeHeaders } from "better-auth/node";
import { auth } from "./auth"; import { auth } from "./auth";
{{/if}} {{/if}}
export async function createContext(opts: CreateExpressContextOptions) { export async function createContext(opts: any) {
{{#if auth}} {{#if auth}}
const session = await auth.api.getSession({ const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers), headers: fromNodeHeaders(opts.req.headers),

View File

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