Skip to content

Commit

Permalink
update: serverjs
Browse files Browse the repository at this point in the history
  • Loading branch information
bintangtobingtheluxeweek committed Jul 26, 2024
1 parent cfcdb5d commit 3299827
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,43 @@ const path = require("path");
const app = express();
const PORT = 3000;

// Use in-memory store for khodamNames
let khodamNames = { names: [] };

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));

// Load monsters data
// Load monster data
let monsters = [];
fs.readFile(path.join(__dirname, "monsters.json"), "utf-8", (err, data) => {
if (err) {
console.error("Error reading monsters.json:", err);
return;
}
monsters = JSON.parse(data);
console.log("Loaded monster names:", monsters);
});

// API endpoint for generating khodam name
// Endpoint to generate a new khodam name
app.post("/generate", (req, res) => {
try {
const name = req.body.name;
const khodamName = generateUniqueKhodamName(name);

const data = JSON.parse(fs.readFileSync("khodamNames.json", "utf-8"));

if (!Array.isArray(data.names)) {
data.names = [];
// Ensure khodamNames.names is an array
if (!Array.isArray(khodamNames.names)) {
khodamNames.names = [];
}

const isDuplicate = data.names.some(
const isDuplicate = khodamNames.names.some(
(khodam) => khodam.nama.toLowerCase() === khodamName.nama.toLowerCase()
);

if (isDuplicate) {
res.status(409).send("Nama Khodam sudah ada, silakan coba lagi.");
} else {
data.names.push(khodamName);
fs.writeFileSync("khodamNames.json", JSON.stringify(data, null, 2));
khodamNames.names.push(khodamName);
// No need to write to file, just update in-memory store
res.json({ khodamName: khodamName });
}
} catch (error) {
Expand All @@ -49,15 +50,30 @@ app.post("/generate", (req, res) => {
}
});

// API endpoint for /api/v1
app.use("/api/v1", require("./api/v1/index"));
// New API endpoint
app.get("/api/v1", (req, res) => {
try {
const name = req.query.name;
if (!name) {
return res.status(400).send("Parameter 'name' is required");
}

const khodamName = generateUniqueKhodamName(name);
res.json({
khodamName: khodamName,
});
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});

// Serve the static index.html file
// Serve the HTML file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});

// Function to generate unique Khodam name
// Function to generate a unique Khodam name
function generateUniqueKhodamName(name) {
if (monsters.length === 0) {
throw new Error("Monsters data is empty");
Expand Down

0 comments on commit 3299827

Please sign in to comment.