1. บทนำ#
การพัฒนา full-stack application ในอดีตใช้เวลาหลายสัปดาห์ แต่วันนี้ด้วย AI tools เช่น ChatGPT สามารถสร้างและ deploy application ที่สมบูรณ์ได้ภายในไม่กี่ชั่วโมง - ฟรี
Tutorial นี้จะสอนวิธี:
- Generate REST API ที่สมบูรณ์ด้วย AI prompts
- Set up database ฟรี (TiDB Serverless)
- Deploy API ไปยัง Vercel (ฟรี)
- Generate frontend โดยใช้ API spec
- Deploy frontend ไปยัง Vercel (ฟรี)
ราคาทั้งหมด: $0
สิ่งที่เราจะสร้าง: E-Commerce API ที่มี:
- Customers (name, email)
- Products (name, price, stock)
- Orders (พร้อม transaction support)
2. สิ่งที่ต้องเตรียม#
ก่อนเริ่ม ให้สร้าง free accounts ดังนี้:
- GitHub ↗ - สำหรับเก็บโค้ด
- Vercel ↗ - สำหรับ deployment
- TiDB Cloud ↗ - สำหรับ database ฟรี
- ChatGPT ↗ - สำหรับ AI code generation
Tools ที่ต้องการ:
- Node.js ติดตั้งในเครื่อง
- Code editor (แนะนำ VS Code)
- Terminal/Command Prompt
3. Part 1: Generate Backend API ด้วย AI#
Step 1: สร้าง Project Folder#
เปิด terminal และรันคำสั่ง:
mkdir ecommerce-api
cd ecommerce-api
npm init -ybashStep 2: AI Prompt สำหรับ Backend#
เปิด ChatGPT ↗ และใช้ prompt นี้:
Create a RESTful API for an e-commerce system using Express.js and mysql2 (no ORM).
Project structure:
ecommerce-api/
├── db.js
├── index.js
├── schema.sql
├── seed.sql
├── .env
└── routes/
├── customers.js
├── products.js
└── orders.js
Database: TiDB
Database name: ecommerce
Tables:
- customers (id, name, email, created_at)
- products (id, name, price, stock, created_at)
- orders (id, customer_id, total, status, created_at)
- order_items (id, order_id, product_id, quantity, price)
Required endpoints for each resource (customers, products, orders):
- GET all - returns array
- GET by id - returns single item or 404
- POST - create new item
- DELETE by id - delete item
For POST /api/orders:
- Use database transactions
- Validate stock before creating order
- Decrease stock when order is placed
- Rollback on any error
Requirements:
- Default port: 3333 (for local development)
- Use parameterized queries (security)
- Return JSON only
- Proper HTTP status codes (200, 201, 404, 500)
- Enable CORS for all origins
- Include error handling
- Export app for Vercel deployment (serverless function)
- Start server only when not in production (NODE_ENV !== 'production')
- .env include DB_SSL
Generate complete code for each file in the exact structure above.
Also provide:
1. npm install command
2. .env file template with database variables
3. Testing commands using curlbashStep 3: Copy โค้ดที่ Generate ได้#
ChatGPT จะ generate โค้ดไฟล์ทั้งหมด ให้สร้างไฟล์แต่ละไฟล์ใน project และ copy โค้ด:
ecommerce-api/
├── db.js
├── index.js
├── schema.sql
├── seed.sql
├── .env
├── package.json
└── routes/
├── customers.js
├── products.js
└── orders.jsbashStep 4: Install Dependencies#
npm install express mysql2 cors dotenvbash4. Part 2: Set Up Database ฟรี (TiDB)#
Step 1: สร้าง TiDB Serverless Cluster#
- ไปที่ tidbcloud.com ↗ และสมัครสมาชิก
- คลิก Create Cluster → TiDB Serverless
- เลือก region ที่ใกล้ที่สุด
- รอให้ cluster สร้างเสร็จ (1-2 นาที)
Step 2: สร้าง Database#
- ใน TiDB Cloud คลิก SQL Editor หรือ Chat to TiDB
- รัน:
CREATE DATABASE ecommerce;
Step 3: รับ Connection Details#
- คลิก Connect บน cluster
- เลือก General connection
- สร้าง password
- Copy connection string
อัปเดตไฟล์ .env ด้วย TiDB credentials:
DB_HOST=your-tidb-host.gateway.prod.aws.tidbcloud.com
DB_PORT=4000
DB_USERNAME=xxx.root
DB_PASSWORD=your-password
DB_DATABASE=ecommerce
DB_SSL=truebashStep 4: รัน Schema และ Seed#
- ใน TiDB Cloud SQL Editor วางเนื้อหาจาก
schema.sql - คลิก Run
- จากนั้นวางและรัน
seed.sql
5. Part 3: ทดสอบ API ในเครื่อง#
เริ่ม Server#
node --watch index.jsbashจะเห็น: Server running on port 3333
ทดสอบด้วย curl (หรือ Postman)#
# Get all customers
curl http://localhost:3333/api/customers
# Create a customer
curl -X POST http://localhost:3333/api/customers \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com"}'
# Get all products
curl http://localhost:3333/api/products
# Create an order
curl -X POST http://localhost:3333/api/orders \
-H "Content-Type: application/json" \
-d '{"customer_id":1,"items":[{"product_id":1,"quantity":2}]}'bashGenerate Postman Collection (ตัวเลือก)#
สำหรับการทดสอบที่ง่ายขึ้น ให้ขอ ChatGPT generate Postman collection:
Generate a Postman collection JSON file for my e-commerce API.
Base URL: http://localhost:3333
Create a complete Postman collection export JSON that I can import.bash- Copy JSON ที่ generate ได้
- Save เป็น
ecommerce-api.postman_collection.json - เปิด Postman → Import → Upload ไฟล์
- จะได้ API requests ทั้งหมดพร้อมทดสอบด้วยคลิกเดียว
6. Part 4: Deploy API ไปยัง Vercel#
สร้าง .gitignore#
node_modules
.envbashPush ไป GitHub ด้วย VS Code#
Initialize git และ push ไป GitHub ด้วย VS Code:
- เปิด project ใน VS Code
- คลิกไอคอน Source Control (หรือกด
Ctrl+Shift+G) - คลิก Initialize Repository
- คลิกไอคอน + ข้าง “Changes” เพื่อ stage การเปลี่ยนแปลงทั้งหมด
- ใส่ commit message:
Initial commit - E-commerce API - คลิก Commit
- คลิกปุ่ม Publish Branch
- ทำตาม prompts เพื่อสร้าง GitHub repository ใหม่ (ทำเป็น Public)
โค้ดตอนนี้อยู่บน GitHub ที่: https://github.com/YOUR_USERNAME/ecommerce-api
Deploy บน Vercel#
- ไปที่ vercel.com ↗
- คลิก Add New → Project
- Import GitHub repository
- คลิก Environment Variables
- เพิ่มตัวแปรทั้งหมดจากไฟล์
.env:- DB_HOST
- DB_PORT
- DB_USERNAME
- DB_PASSWORD
- DB_DATABASE
- DB_SSL
- คลิก Deploy
API ตอนนี้ live ที่: https://your-project.vercel.app
ทดสอบ: https://your-project.vercel.app/api/customers
7. Part 5: Generate API Reference#
ก่อน generate frontend ต้องมี API specification ที่สมบูรณ์ก่อน ให้ AI generate จากโค้ด!
Prompt สำหรับ API Reference#
เปิด ChatGPT และวาง prompt นี้พร้อมกับ route files:
Generate a complete API reference documentation for my e-commerce API.
Here are my route files:
=== File: index.js ===
[PASTE CONTENTS of index.js]
=== File: routes/customers.js ===
[PASTE CONTENTS of routes/customers.js]
=== File: routes/products.js ===
[PASTE CONTENTS of routes/products.js]
=== File: routes/orders.js ===
[PASTE CONTENTS of routes/orders.js]
Generate documentation in **markdown format** with:
- Base URL: [Vercel Based URL, for example: https://your-project.vercel.app]
- Each endpoint grouped by resource (CUSTOMERS, PRODUCTS, ORDERS)
- For each endpoint include:
- HTTP method and path (e.g., GET /api/customers)
- Description
- Request body (if POST/PUT)
- Response format with actual example data
- HTTP status codes
Make it easy to copy-paste for frontend development.bashบันทึก API Reference#
ChatGPT จะ output API reference ที่สะอาด บันทึกไว้ - จะต้องใช้ใน frontend prompt
ควรจะมีหน้าตาประมาณนี้ (endpoint จริงอาจต่างกัน):
Base URL: https://your-project.vercel.app
=== CUSTOMERS ===
GET /api/customers
Description: Get all customers
Response: [{"id":1,"name":"Karn Yong","email":"karn@example.com","created_at":"..."}]
...bash8. Part 6: Generate Frontend ด้วย AI#
เมื่อ API ถูก deploy แล้ว มา generate Next.js frontend กันเถอะ
AI Prompt สำหรับ Frontend#
เปิด ChatGPT conversation ใหม่และใช้ prompt:
Create a Next.js 15 frontend for an e-commerce system using App Router.
Use these API endpoints:
[PASTE CONTENTS of API Reference]
Requirements:
- Use Tailwind CSS for styling (shadcn/ui components preferred)
- Create pages: Customers, Products, Orders, Create Order
- Display data in tables
- Simple forms to add data
- Show loading states and error messages
- Use fetch for API calls
- Keep it simple and clean
File structure:
- app/page.js - home/dashboard
- app/customers/page.js
- app/products/page.js
- app/orders/page.js
- app/layout.js - with navigation
- components/ - reusable components
- lib/api.js - API functions
Provide complete code for all files.bashสร้าง Frontend Project#
npx create-next-app@15 ecommerce-frontend --tailwind --appbashเมื่อถูกถาม ให้เลือก No สำหรับ TypeScript
cd ecommerce-frontend
npm installbashCopy โค้ดที่ ChatGPT generate มาใส่ใน project
อัปเดต API URL#
อย่าลืมแทนที่ https://your-project.vercel.app ด้วย deployed API URL จริงใน lib/api.js
ทดสอบ Frontend ในเครื่อง#
ก่อน deploy ให้ทดสอบ frontend ในเครื่องก่อน:
npm run devbash- เปิด http://localhost:3000 ↗ ใน browser
- ทดสอบทุกหน้า: Customers, Products, Orders
- ลองสร้างข้อมูลใหม่
- ตรวจสอบว่า API calls ทำงานถูกต้อง
ถ้าทำงานได้ทั้งหมด พร้อม deploy แล้ว!
กด Ctrl+C เพื่อหยุด dev server เมื่อทดสอบเสร็จ
9. Part 7: Deploy Frontend ไปยัง Vercel#
Push Frontend ไป GitHub ด้วย VS Code#
- สร้าง repository ใหม่บน GitHub (ทำเป็น Public)
- เปิด
ecommerce-frontendใน VS Code - คลิกไอคอน Source Control (หรือกด
Ctrl+Shift+G) - คลิก Initialize Repository
- Stage การเปลี่ยนแปลงทั้งหมด (คลิกไอคอน +)
- ใส่ commit message:
Initial commit - E-commerce frontend - คลิก Commit
- คลิก Publish Branch
- ทำตาม prompts เพื่อ publish ไปยัง GitHub
Deploy บน Vercel#
- ไปที่ Vercel → Add New → Project
- Import
ecommerce-frontendrepository - คลิก Deploy (ไม่ต้องการ environment variables)
Frontend ตอนนี้ live ที่: https://your-frontend-project.vercel.app
10. Summary#
สิ่งที่สร้างไปด้วย $0:
| Component | Technology | Cost |
|---|---|---|
| API | Express.js + mysql2 | Free |
| Database | TiDB Serverless | Free (5GB) |
| Backend Hosting | Vercel | Free (100GB bandwidth) |
| Frontend | Next.js 15 | Free |
| Frontend Hosting | Vercel | Free (100GB bandwidth) |
Next Steps:
- Add authentication with JWT
- Implement pagination
- Add search and filtering
- Set up custom domain
- Add payment processing
Tips สำหรับ AI Prompts ที่ดีกว่า:
- ระบุ tech stack อย่างชัดเจน
- รวม sample request/response formats
- ระบุ security requirements
- ขอ error handling
- ขอ file structure ที่สมบูรณ์
กุญแจสำคัญในการ build ด้วย AI คือการให้ specifications ที่ชัดเจน Prompt ที่ละเอียดยิ่ง โค้ดที่ generate ได้ยิ่งดี
Happy building!