File size: 1,480 Bytes
419f78f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require("express");
const app = express();

app.use(express.json());

// --------------------
// PORT CONFIG
// --------------------
const PORT = process.env.PORT || 5900;

// --------------------
// LIST CHOICES
// --------------------
const choices = [
  { id: 1, name: "Basic PIN" },
  { id: 2, name: "Secure PIN" },
  { id: 3, name: "Random PIN" }
];

app.get("/list", (req, res) => {
  res.json({
    success: true,
    choices
  });
});

// --------------------
// GENERATE PIN
// --------------------
function generatePin() {
  return Math.floor(1000 + Math.random() * 9000).toString();
}

app.get("/generate-pin", (req, res) => {
  const choice = parseInt(req.query.choice);

  if (!choice) {
    return res.json({
      success: false,
      message: "Missing choice number"
    });
  }

  let pin = generatePin();

  // optional behavior per choice
  if (choice === 2) {
    while (/(\d)\1/.test(pin)) {
      pin = generatePin();
    }
  }

  if (choice === 3) {
    pin = generatePin();
  }

  res.json({
    success: true,
    choice,
    pin
  });
});

// --------------------
// EXIT SERVER
// --------------------
app.get("/api/exit", (req, res) => {
  res.json({
    success: true,
    message: "Server exiting..."
  });

  console.log("Server exiting...");
  process.exit(0);
});

// --------------------
// START SERVER (NO HOST BIND)
// --------------------
app.listen(PORT, () => {
  console.log("Server running on http://127.0.0.1:5900");
});