295 lines
5.3 KiB
Markdown
295 lines
5.3 KiB
Markdown
# 🚀 AccManager Backend - Complete Setup Guide
|
|
|
|
## ⚠️ Pre-requisites
|
|
|
|
### 1. Install Node.js & npm
|
|
|
|
**Download từ:** https://nodejs.org/
|
|
**Khuyến khích:** LTS version (v18 hoặc mới hơn)
|
|
|
|
#### Kiểm tra installation:
|
|
```bash
|
|
node --version
|
|
npm --version
|
|
```
|
|
|
|
Expected output:
|
|
```
|
|
v18.* (or newer)
|
|
9.* (or newer)
|
|
```
|
|
|
|
### 2. Verify SQL Server Connection
|
|
|
|
Trước khi chạy backend, kiểm tra SQL Server:
|
|
|
|
```bash
|
|
ping 172.20.235.176
|
|
```
|
|
|
|
Nếu không ping được, kiểm tra:
|
|
- SQL Server đang chạy
|
|
- Firewall cho phép port 1433
|
|
- Network connectivity
|
|
|
|
---
|
|
|
|
## 📥 Setup Steps
|
|
|
|
### Step 1: Install Node Packages
|
|
|
|
```bash
|
|
cd d:\RoboticsSource\AccManager
|
|
npm install
|
|
```
|
|
|
|
Wait cho tới khi mô tả xuất hiện `added X packages`
|
|
|
|
### Step 2: Run Backend Server
|
|
|
|
```bash
|
|
npm start
|
|
```
|
|
|
|
**Expected Output:**
|
|
```
|
|
========================================
|
|
AccManager Backend Server
|
|
========================================
|
|
✓ Server running on http://localhost:3000
|
|
✓ Database: AccManager
|
|
✓ Default admin: admin / admin
|
|
|
|
API Endpoints:
|
|
POST /api/auth/login
|
|
GET /api/database/info
|
|
GET /api/users
|
|
GET /api/applications
|
|
GET /api/accounts/user/:userId
|
|
========================================
|
|
```
|
|
|
|
### Step 3: Test Connection
|
|
|
|
Mở terminal mới, chạy:
|
|
|
|
```bash
|
|
curl http://localhost:3000/api/health
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"status": "OK",
|
|
"database": "Connected"
|
|
}
|
|
```
|
|
|
|
### Step 4: Test Database Info
|
|
|
|
```bash
|
|
curl http://localhost:3000/api/database/info
|
|
```
|
|
|
|
---
|
|
|
|
## 📝 Database Structure
|
|
|
|
### Database Name: `AccManager`
|
|
|
|
#### Tables:
|
|
1. **Users** (1 admin account)
|
|
- Username: admin
|
|
- Password: admin
|
|
- Role: admin
|
|
|
|
2. **Applications** (4 sample apps)
|
|
- AWS
|
|
- GitHub
|
|
- Google Workspace
|
|
- Nginx Proxy
|
|
|
|
3. **Accounts** (empty, ready to use)
|
|
|
|
4. **AuditLog** (empty, for logging)
|
|
|
|
---
|
|
|
|
## 🧪 Test API Endpoints
|
|
|
|
### Test 1: Login
|
|
```bash
|
|
curl -X POST http://localhost:3000/api/auth/login \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"username\":\"admin\",\"password\":\"admin\"}"
|
|
```
|
|
|
|
### Test 2: Get Users
|
|
```bash
|
|
curl http://localhost:3000/api/users
|
|
```
|
|
|
|
### Test 3: Get Applications
|
|
```bash
|
|
curl http://localhost:3000/api/applications
|
|
```
|
|
|
|
### Test 4: View Database Info
|
|
```bash
|
|
curl http://localhost:3000/api/database/info
|
|
```
|
|
|
|
---
|
|
|
|
## 🔌 Frontend Integration
|
|
|
|
Update your frontend to connect to the backend:
|
|
|
|
### Option 1: Update app.js
|
|
|
|
```javascript
|
|
const API_URL = 'http://localhost:3000/api';
|
|
|
|
// Update login function
|
|
async function login(username, password) {
|
|
const response = await fetch(`${API_URL}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
localStorage.setItem('currentUser', JSON.stringify(data.user));
|
|
window.location.href = './pages/accounts.html';
|
|
} else {
|
|
alert('Login failed: ' + data.message);
|
|
}
|
|
}
|
|
|
|
// Update get accounts function
|
|
async function getAccounts(userId) {
|
|
const response = await fetch(`${API_URL}/accounts/user/${userId}`);
|
|
const data = await response.json();
|
|
return data.data || [];
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 📦 Project Structure
|
|
|
|
```
|
|
d:\RoboticsSource\AccManager\
|
|
├── server.js # Backend server (Node.js)
|
|
├── package.json # Dependencies
|
|
├── .env # Configuration
|
|
├── DATABASE_SETUP.md # This file
|
|
├── database/
|
|
│ └── setup.sql # SQL setup script
|
|
├── index.html # Frontend entry
|
|
├── pages/
|
|
│ ├── login.html
|
|
│ ├── accounts.html
|
|
│ ├── applications.html
|
|
│ └── index.html
|
|
└── js/
|
|
└── app.js # Frontend logic
|
|
```
|
|
|
|
---
|
|
|
|
## 🔒 Security Notes
|
|
|
|
⚠️ **For Development Only:**
|
|
- Admin password is hardcoded as "admin"
|
|
- SQL credentials in code (not recommended for production)
|
|
- CORS enabled for all origins
|
|
|
|
### For Production:
|
|
1. Use environment variables
|
|
2. Hash passwords with bcrypt
|
|
3. Implement JWT authentication
|
|
4. Use firewalls and VPNs
|
|
5. Enable SSL/TLS
|
|
|
|
---
|
|
|
|
## 🐛 Troubleshooting
|
|
|
|
### Error: "Cannot find module 'express'"
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
### Error: "Connection failed"
|
|
Check:
|
|
- SQL Server running
|
|
- IP address correct: 172.20.235.176
|
|
- Port 1433 accessible
|
|
- Username/password correct
|
|
|
|
### Error: "Port 3000 already in use"
|
|
```bash
|
|
# Change port in .env
|
|
PORT=3001
|
|
|
|
# Then restart server
|
|
npm start
|
|
```
|
|
|
|
### Error: "CORS error in browser"
|
|
This is normal during development. The backend already has CORS enabled.
|
|
|
|
---
|
|
|
|
## 📊 Database Credentials
|
|
|
|
```
|
|
Server: 172.20.235.176
|
|
Database: AccManager
|
|
User: sa
|
|
Password: robotics@2020
|
|
```
|
|
|
|
---
|
|
|
|
## ✅ Verification Checklist
|
|
|
|
- [ ] Node.js installed
|
|
- [ ] npm packages installed (`npm install`)
|
|
- [ ] Backend server running (`npm start`)
|
|
- [ ] Can access http://localhost:3000/api/health
|
|
- [ ] Can login with admin/admin
|
|
- [ ] Database shows tables and statistics
|
|
- [ ] Frontend connects to backend
|
|
|
|
---
|
|
|
|
## 📚 Additional Commands
|
|
|
|
### Install Development Tools
|
|
```bash
|
|
npm install -D nodemon
|
|
npm run dev # Auto-restart on code changes
|
|
```
|
|
|
|
### Check npm packages
|
|
```bash
|
|
npm list
|
|
```
|
|
|
|
### Update packages
|
|
```bash
|
|
npm update
|
|
```
|
|
|
|
### Clear npm cache
|
|
```bash
|
|
npm cache clean --force
|
|
```
|
|
|
|
---
|
|
|
|
**For Support:** Check server logs for error messages
|