Dự án phục vụ bài thực hành Client–Server. Repo này có 2 backend (cùng API To-Do) và 1 web client tối giản:
- Node.js: Express + Sequelize + SQLite (mặc định
:3001) - ASP.NET Core 8: Minimal API + EF Core + SQLite (khuyên chạy
:3002) - web-client: trang HTML/JS gọi API
Hai backend dùng cùng thiết kế API (
/api/todos). Chạy một trong hai hoặc cả hai (khác cổng).
demo-backend/
├─ aspnet-demo/ # ASP.NET Core 8 Minimal API + EF Core + SQLite
│ ├─ Program.cs
│ ├─ aspnet-demo.csproj
│ ├─ Properties/
│ │ └─ launchSettings.json
│ └─ todos.db # sinh ra khi chạy
│
├─ nodejs-demo/ # Node.js + Express + Sequelize + SQLite
│ ├─ models/
│ │ └─ Todo.js
│ ├─ server.js
│ ├─ package.json
│ └─ todos.db # sinh ra khi chạy
│
├─ web-client/ # Client HTML/JS (tùy chọn)
│ ├─ index.html
│ ├─ styles.css
│ └─ script.js
│
├─ demo-backend.sln # Solution mở bằng Visual Studio
├─ ghichu-server # Ghi chú server
└─ README.md
cd nodejs-demo
npm install # chỉ cần khi mới tải mã nguồn về
npm run startMở trình duyệt hoặc dùng curl:
http://localhost:3001/api/todos
CMD:
cd aspnet-demo
set ASPNETCORE_URLS=http://localhost:3002 && dotnet runPowerShell:
cd aspnet-demo
$env:ASPNETCORE_URLS="http://localhost:3002"; dotnet runMở trình duyệt hoặc dùng curl:
http://localhost:3002/api/todos
---
## Yêu cầu môi trường
- **Node.js ≥ 16** · kiểm tra: `node -v`
- **.NET SDK ≥ 8.0** · kiểm tra: `dotnet --version`
- **VS Code** (khuyên dùng), **Postman**/**curl** để test API
> Nếu mới cài, **mở terminal mới** để PATH cập nhật.
---
## 1) Chạy backend Node.js (Express + Sequelize + SQLite)
```bash
cd nodejs-demo
# Cài thư viện (lần đầu)
npm install
# Chạy server
npm run start
# hoặc reload khi sửa file
npm run dev
- Mặc định API:
http://localhost:3001 package.jsoncần có:"type": "module", "scripts": { "start": "node server.js", "dev": "node --watch server.js" }
GET /api/todosGET /api/todos/{id}POST /api/todos(body:{ "title": "..." })PUT /api/todos/{id}(body:{ "title"?: string, "completed"?: boolean })POST /api/todos/{id}/toggleDELETE /api/todos/{id}
curl http://localhost:3001/api/todos
curl -X POST http://localhost:3001/api/todos -H "Content-Type: application/json" -d "{"title":"Đọc tài liệu HTTP"}"
curl -X PUT http://localhost:3001/api/todos/1 -H "Content-Type: application/json" -d "{"title":"Đã sửa","completed":true}"
curl -X POST http://localhost:3001/api/todos/1/toggle
curl -X DELETE http://localhost:3001/api/todos/1Lần chạy đầu, API sẽ seed một vài Todo mẫu nếu DB trống.
cd aspnet-demo
dotnet restore
dotnet runMặc định Kestrel dùng cổng trong Properties/launchSettings.json. Bạn có thể:
Mở aspnet-demo/Properties/launchSettings.json và đặt:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:3002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}Chạy:
dotnet run
# → Now listening on: http://localhost:3002- PowerShell
$env:ASPNETCORE_URLS="http://localhost:3002"; dotnet run --no-launch-profile
- CMD
set ASPNETCORE_URLS=http://localhost:3002 && dotnet run --no-launch-profile
GET /api/todosGET /api/todos/{id}POST /api/todosPUT /api/todos/{id}POST /api/todos/{id}/toggleDELETE /api/todos/{id}
curl http://localhost:3002/api/todos
curl -X POST http://localhost:3002/api/todos -H "Content-Type: application/json" -d "{"title":"Ôn Node.js & ASP.NET Core"}"
curl -X PUT http://localhost:3002/api/todos/1 -H "Content-Type: application/json" -d "{"title":"Đã sửa","completed":true}"
curl -X POST http://localhost:3002/api/todos/1/toggle
curl -X DELETE http://localhost:3002/api/todos/1Lần chạy đầu, API cũng seed một vài Todo mẫu.
- Vào
web-client/, mởindex.html(nên chạy bằng extension Live Server trong VS Code). - Nếu client có biến cấu hình
API_BASE_URL, đặt về:http://localhost:3001(Node), hoặchttp://localhost:3002(ASP.NET)
- Đảm bảo backend tương ứng đang chạy. CORS đã mở sẵn.
Tạo collection với các request giống phần curl. (Hoặc import file collection nếu cung cấp kèm repo.)
Tạo file test.http và dán:
@baseUrl = http://localhost:3001 # hoặc 3002
GET {{baseUrl}}/api/todos
POST {{baseUrl}}/api/todos
Content-Type: application/json
{ "title": "Ví dụ REST Client" }
PUT {{baseUrl}}/api/todos/1
Content-Type: application/json
{ "title": "Đã sửa", "completed": true }
POST {{baseUrl}}/api/todos/1/toggle
DELETE {{baseUrl}}/api/todos/1aspnet-demo/.gitignore
bin/
obj/
*.user
*.suo
*.sln.docstates
.vscode/
.idea/
*.db
*.sqlite
*.sqlite3
logs/
*.log
nodejs-demo/.gitignore
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
.env
.DS_Store
.vscode/
*.db
*.sqlite
*.sqlite3
-
Node:
npm run startbáo thiếu script
→ Thêm vàonodejs-demo/package.json:"scripts": { "start": "node server.js", "dev": "node --watch server.js" }
-
Node:
Cannot find module '.../server.js'
→ Đảm bảoserver.jsnằm trực tiếp trongnodejs-demo/(không để trong thư mục con), hoặc chỉnh script đúng đường dẫn. -
ASP.NET chạy sai cổng dù đã đặt
ASPNETCORE_URLS
→ Bạn vẫn đang dùng launchSettings.json. Dùng--no-launch-profilehoặc sửaapplicationUrltrong file đó. -
SQLITE_BUSY(DB bị khóa)
→ Đóng app đang mở file DB (DB Browser, VS Code preview…), chạy lại.
Mã nguồn phục vụ học tập/thử nghiệm. Bạn có thể fork và dùng cho mục đích cá nhân/giảng dạy.
Tác giả: Nguyễn Lê Quốc Lâm – Dalat University (Backend in training).
Nếu thấy hữu ích, hãy ⭐ repo để ủng hộ!