Okk / serverapi.js
Jsjsjj8383's picture
Rename server.js to serverapi.js
bbc5f95 verified
Raw
History Blame Contribute Delete
1.48 kB
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");
});