const express = require('express'); const cors = require('cors'); const app = express(); // Middleware app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); // In-Memory Database Simulation let shipments = []; // Helper engine to generate a unique tracking code function generateTrackingCode() { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let segment1 = ''; let segment2 = ''; for (let i = 0; i < 4; i++) { segment1 += chars.charAt(Math.floor(Math.random() * chars.length)); segment2 += chars.charAt(Math.floor(Math.random() * chars.length)); } return `GL-${segment1}-${segment2}`; } // ==================== REST API ENDPOINTS ==================== // Endpoint 1: Create a shipment & issue a tracking code app.post('/api/shipments', (req, res) => { const { sender, receiver, location, status } = req.body; if (!sender || !receiver) { return res.status(400).json({ message: "Sender and Receiver fields are required." }); } let trackingCode = generateTrackingCode(); // Safety check to guarantee uniqueness within our array while (shipments.some(s => s.trackingCode === trackingCode)) { trackingCode = generateTrackingCode(); } const newShipment = { trackingCode, sender, receiver, location: location || "Sorting Facility", status: status || "Pending", updatedAt: new Date() }; shipments.push(newShipment); res.status(201).json(newShipment); }); // Endpoint 2: Track an existing shipment by its code app.get('/api/shipments/:code', (req, res) => { const shipment = shipments.find(s => s.trackingCode.toUpperCase() === req.params.code.toUpperCase()); if (!shipment) { return res.status(404).json({ message: "Shipment tracking code not found." }); } res.json(shipment); }); // ==================== MONOLITHIC FRONTEND ROUTE ==================== // Serves the full user interface directly on the root URL app.get('/', (req, res) => { res.send(`
Worldwide Logistics & Forwarding