Files
ManagerAccount/SETUP_GUIDE.md
2026-03-27 09:56:26 +07:00

5.3 KiB

🚀 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:

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:

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

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

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:

curl http://localhost:3000/api/health

Response:

{
  "status": "OK",
  "database": "Connected"
}

Step 4: Test Database Info

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

curl -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"admin\",\"password\":\"admin\"}"

Test 2: Get Users

curl http://localhost:3000/api/users

Test 3: Get Applications

curl http://localhost:3000/api/applications

Test 4: View Database Info

curl http://localhost:3000/api/database/info

🔌 Frontend Integration

Update your frontend to connect to the backend:

Option 1: Update app.js

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'"

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"

# 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

npm install -D nodemon
npm run dev    # Auto-restart on code changes

Check npm packages

npm list

Update packages

npm update

Clear npm cache

npm cache clean --force

For Support: Check server logs for error messages