# Hono Service (Node.js Runtime) - AI Generator Guidelines

These guidelines are for generating Hono microservices that run with **Node.js runtime** (pnpm, npm, yarn).

---

## Entry Point Template (src/index.ts)

**Basic REST API:**
```ts
import '@crossdelta/telemetry'

import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

// Your routes here
app.get('/', (c) => c.text('Hello Hono!'))

serve({
  fetch: app.fetch,
  port
}, (info) => {
  console.log(`Server is running on http://localhost:${info.port}`)
})
```

**Event Consumer (NATS) - Multiple Streams:**
```ts
import '@crossdelta/telemetry'

import { consumeJetStreamStreams, ensureJetStreamStreams } from '@crossdelta/cloudevents'
import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

serve({
  fetch: app.fetch,
  port
}, (info) => {
  console.log(`Server is running on http://localhost:${info.port}`)
})

await ensureJetStreamStreams({
  streams: [
    { stream: 'ORDERS', subjects: ['orders.*'] },
    { stream: 'CUSTOMERS', subjects: ['customers.*'] },
  ]
})

consumeJetStreamStreams({
  streams: ['ORDERS', 'CUSTOMERS'],
  consumer: 'my-service',
  discover: './src/events/**/*.event.ts',
})
```

**Event Publisher (REST + CloudEvents):**
```ts
import '@crossdelta/telemetry'

import { publish } from '@crossdelta/cloudevents'
import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const port = Number(process.env.PORT || process.env.MY_SERVICE_PORT) || 8080
const app = new Hono()

app.get('/health', (c) => c.json({ status: 'ok' }))

app.post('/orders', async (c) => {
  const data = await c.req.json()
  await publish('orders.created', data, { source: 'my-service' })
  return c.json({ success: true })
})

serve({
  fetch: app.fetch,
  port
}, (info) => {
  console.log(`Server is running on http://localhost:${info.port}`)
})
```

---

## Key Points

- ✅ Use `serve()` from `@hono/node-server` (NOT `Bun.serve`)
- ✅ Import `serve` from `@hono/node-server`
- ✅ Import telemetry FIRST (before any other imports)
- ✅ Use the port env var name provided in the prompt (e.g., `API_GATEWAY_PORT`)
- ✅ Always include `/health` endpoint
- ✅ Include callback with console.log for server startup
- ✅ Keep routes simple and delegate logic to use-cases
- ✅ Use `ensureJetStreamStreams` + `consumeJetStreamStreams` for multiple event types

---

## Dev Command

```json
{
  "scripts": {
    "dev": "tsx watch src/index.ts"
  }
}
```
