How to programmatically serve a tanstack start application bundled with nitro on bun.js #4176
-
|
Question. How can I programmatically run a nitro app on bun.js? I reviewed both tanstack start's and nitro's documentation and I couldn't figure out how to programmatically serve the output. I have a cli that can "serve" a tanstack start app. The closest documentation I've come across is using nitro's node deployment model via this documentation But I'm not sure if this is the recommended approach for a Bun runtime as well. There's a separate documentation for bun here but it doesn't show how to programmatically run it - it invokes bun instead of running it programmatically in code. Any help is much appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The output at // cli/serve.ts
import { parseArgs } from "util"
const { values } = parseArgs({
args: Bun.argv,
options: {
port: { type: "string", default: "3000" },
host: { type: "string", default: "0.0.0.0" },
},
strict: false,
allowPositionals: true,
})
process.env.PORT = values.port
process.env.HOST = values.host
await import("./.output/server/index.mjs")
console.log(`serving on http://${values.host}:${values.port}`)Run it: Env vars Nitro reads: |
Beta Was this translation helpful? Give feedback.
The output at
.output/server/index.mjsfrom thebunpreset is self-contained: it callsBun.serve()the moment it gets imported. "Programmatic" here is just a dynamic import with the env vars set beforehand.Run it:
bun cli/serve.ts --port 8080.Env vars Nitro reads:
P…