Backend API for HanGo Korean learning app.
- Framework: Hono (fast, lightweight web framework)
- Runtime: Node.js
- Database: PostgreSQL
- Language: TypeScript
- Deployment: Railway
npm installCopy .env.example to .env and fill in the values:
cp .env.example .envRequired variables:
DATABASE_URL: PostgreSQL connection stringJWT_SECRET: Secret key for JWT tokensOPENAI_API_KEY: OpenAI API key for AI chat featuresPORT: Server port (default: 8787)
Run the schema on your PostgreSQL database:
psql $DATABASE_URL < schema.sqlOr copy the contents of schema.sql and run it in your database client.
npm run devServer will start on http://localhost:8787
# Install Railway CLI
npm install -g @railway/cli
# Login
railway login
# Create new project
railway initIn Railway dashboard:
- Click "New" → "Database" → "PostgreSQL"
- Wait for provisioning
- Copy the
DATABASE_URLfrom the database service
railway variables set JWT_SECRET=your-secret-key
railway variables set OPENAI_API_KEY=sk-your-key# Link to Railway project
railway link
# Deploy
railway upAfter first deployment:
# Connect to Railway shell
railway run
# Run schema
psql $DATABASE_URL < schema.sqlOr use Railway's database query interface to run schema.sql.
railway domainYour API will be available at the provided URL.
POST /auth/signup- Create new userPOST /auth/login- Login userGET /auth/me- Get current user
GET /vocabulary/session- Get learning session (10 words)POST /vocabulary/answer- Submit answerGET /vocabulary/progress- Get user progressGET /vocabulary/search?q=...- Search vocabularyGET /vocabulary/all- Get all vocabulary
POST /ai/chat- Send message to AI friendGET /ai/friends- Get available AI friendsPOST /ai/translate- Translate text to Korean
npm run dev- Start development server with hot reloadnpm run build- Build TypeScript to JavaScriptnpm start- Run production server
src/
├── db.ts # PostgreSQL connection pool
├── index.ts # Hono app setup and routes
├── server.ts # Node.js server entry point
├── types/
│ └── index.ts # TypeScript type definitions
├── utils/
│ ├── jwt.ts # JWT utilities
│ └── password.ts # Password hashing
└── routes/
├── auth.ts # Authentication routes
├── vocabulary.ts # Vocabulary routes
└── ai-chat.ts # AI chat routes
- Uses ESM modules (
type: "module"in package.json) - All imports must include
.jsextension - TypeScript compiles to JavaScript in
dist/folder - PostgreSQL uses
$1, $2, ...placeholder syntax - Railway automatically sets
DATABASE_URLfor PostgreSQL service
This backend was migrated from Cloudflare Workers to Railway:
Changes:
- D1 (SQLite) → PostgreSQL
- Cloudflare Workers bindings → Environment variables
?placeholders →$1, $2, ...placeholdersdatetime('now')→NOW()- Added Node.js server entry point
Benefits:
- No OpenAI region restrictions
- More mature PostgreSQL features
- Easier local development
- Better TypeScript support