can_sms/server/server.js
2023-11-11 19:57:35 +01:00

75 lines
2.1 KiB
JavaScript

import express from "express";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const app = express();
const port = 8080;
app.use(express.json());
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let basedir = __dirname + "/../client";
app.use("/", express.static(basedir + "/"));
app.use("/bootstrap", express.static(basedir + "/../node_modules/bootstrap/dist/"));
let ranNum;
app.listen(port, () => {
console.log(`Server gestartet auf http://localhost:${port}/`);
ranNum = random(1, 10);
});
app.get('/guess/:guess', (req, res) => {
let guess = Number(req.params.guess);
console.log(ranNum);
if (Number.isNaN(guess)) {
res.status(200).send({
answer: 'Please enter a valid number!',
win: false
});
}
else if (guess > ranNum) {
res.status(200).send({
answer: 'Your guess was to high!',
win: false
});
}
else if (guess < ranNum) {
res.status(200).send({
answer: 'Your guess was to low!',
win: false
});
}
else {
res.status(200).send({
answer: 'Your guess was right!',
win: true
});
}
});
app.post('/reset', (req, res) => {
const min = Number(req.body.min);
const max = Number(req.body.max);
if (Number.isNaN(min || Number.isNaN(max) || min > max || min <= 0 || max <= 0)) {
return res.status(200).send({
message: 'Invalid input'
});
}
ranNum = random(min, max);
res.status(200).send({
message: 'A new number has been set.'
});
});
app.post('/cheat', (req, res) => {
const password = "bruh";
let cheat = req.body.password;
if (password === cheat) {
res.status(200).send({
message: `Random number was: ${ranNum} Next time you'll get it`
});
}
else {
res.status(200).send({
message: "Incorrect password"
});
}
});
function random(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
//# sourceMappingURL=server.js.map