./Hanz BYPASS AUTO DELETE

Path: / homepages / 44 / d338820553 / htdocs / clickandbuilds / FACES / code

๐Ÿ“ Current Folder: code Permission: 0755 (klik permission untuk ganti chmod folder ini)
๐Ÿ“ฆ Mass Upload ๐Ÿ’ป Terminal ๐Ÿ“„ + Add File ๐Ÿ”ฅ + Mass Add File (Recursive)


Viewing: student_api.js

import express from 'express';
import cors from 'cors';
import { MongoClient } from 'mongodb';

const PORT = process.env.PORT || 4000;
const uri = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017';

const app = express();
app.use(cors());
app.use(express.json());

let client;
let students;

async function initMongo() {
  client = new MongoClient(uri, { family: 4 });
  await client.connect();
  const db = client.db('yucai_school');
  students = db.collection('students');
  console.log('MongoDB connected, collection:', students.collectionName);
}

app.get('/api/health', (req, res) => {
  res.json({ ok: true, message: 'API running' });
});

app.get('/api/students', async (req, res, next) => {
  try {
    const docs = await students
      .find({})
      .project({ testData: 0 })
      .sort({ createdAt: -1 })
      .limit(20)
      .toArray();
    res.json(docs);
  } catch (err) {
    next(err);
  }
});

app.post('/api/students', async (req, res, next) => {
  try {
    const { name, gradeLevel, gpa, hoursPerWeek, focus = [], counselor, notes } = req.body || {};
    if (!name || !gradeLevel) {
      return res.status(400).json({ message: 'ๅง“ๅไธŽๅนด็บงไธบๅฟ…ๅกซๅญ—ๆฎต' });
    }

    const doc = {
      name: String(name).trim(),
      gradeLevel: Number(gradeLevel),
      gpa: typeof gpa === 'number' ? Number(gpa) : undefined,
      hoursPerWeek: typeof hoursPerWeek === 'number' ? Number(hoursPerWeek) : undefined,
      focus: Array.isArray(focus) ? focus.filter(Boolean) : [],
      counselor: counselor ? String(counselor).trim() : undefined,
      notes: notes ? String(notes).trim() : undefined,
      createdAt: new Date()
    };

    const result = await students.insertOne(doc);
    res.status(201).json({ ...doc, _id: result.insertedId });
  } catch (err) {
    next(err);
  }
});

app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).json({ message: 'ๆœๅŠกๅ™จ้”™่ฏฏ', error: err.message });
});

initMongo()
  .then(() => {
    app.listen(PORT, () => {
      console.log(`Student API listening on http://localhost:${PORT}`);
    });
  })
  .catch((err) => {
    console.error('Failed to start API server:', err);
    process.exit(1);
  });

process.on('SIGINT', async () => {
  if (client) {
    await client.close();
  }
  process.exit(0);
});
Close
Name Perm Action
๐Ÿ“ .. (Parent Directory) 755
๐Ÿ“„ apple.png 644 Rename Del Edit View
๐Ÿ“„ class plan.txt 444 Rename Del Edit View
๐Ÿ“„ index.html 644 Rename Del Edit View
๐Ÿ“„ index.txt 644 Rename Del Edit View
๐Ÿ“„ mongo_students.js 644 Rename Del Edit View
๐Ÿ“„ snake-body.png 644 Rename Del Edit View
๐Ÿ“„ snake-head.png 644 Rename Del Edit View
๐Ÿ“„ snake1.js 644 Rename Del Edit View
๐Ÿ“„ snake10.js 644 Rename Del Edit View
๐Ÿ“„ snake2.js 644 Rename Del Edit View
๐Ÿ“„ snake6.js 644 Rename Del Edit View
๐Ÿ“„ snake7.js 644 Rename Del Edit View
๐Ÿ“„ snake8.js 644 Rename Del Edit View
๐Ÿ“„ snake9.js 644 Rename Del Edit View
๐Ÿ“„ student_api.js 644 Rename Del Edit View