diff --git a/.changeset/brown-fans-wave.md b/.changeset/brown-fans-wave.md new file mode 100644 index 0000000..489d10a --- /dev/null +++ b/.changeset/brown-fans-wave.md @@ -0,0 +1,5 @@ +--- +"create-better-t-stack": patch +--- + +fix express server orpc route diff --git a/apps/cli/templates/api/orpc/server/base/src/lib/context.ts.hbs b/apps/cli/templates/api/orpc/server/base/src/lib/context.ts.hbs index ff1ab35..2bd35d4 100644 --- a/apps/cli/templates/api/orpc/server/base/src/lib/context.ts.hbs +++ b/apps/cli/templates/api/orpc/server/base/src/lib/context.ts.hbs @@ -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), diff --git a/apps/cli/templates/backend/express/src/index.ts.hbs b/apps/cli/templates/backend/express/src/index.ts.hbs index d5fd975..0899744 100644 --- a/apps/cli/templates/backend/express/src/index.ts.hbs +++ b/apps/cli/templates/backend/express/src/index.ts.hbs @@ -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}`); }); diff --git a/apps/web/src/app/(home)/new/page.tsx b/apps/web/src/app/(home)/new/page.tsx index fea55d0..759b3ff 100644 --- a/apps/web/src/app/(home)/new/page.tsx +++ b/apps/web/src/app/(home)/new/page.tsx @@ -6,7 +6,7 @@ import StackArchitect from "../_components/StackArchitech"; export default function FullScreenStackArchitect() { return ( -