79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
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: string = __dirname + "/../client";
|
|
app.use("/", express.static(basedir + "/"));
|
|
app.use("/bootstrap", express.static(basedir + "/../node_modules/bootstrap/dist/"));
|
|
let ranNum: number;
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server gestartet auf http://localhost:${port}/`);
|
|
ranNum = random(1, 10);
|
|
});
|
|
|
|
app.get('/guess/:guess', (req, res) => {
|
|
let guess: number = 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 = Number(req.body.min);
|
|
const max: number = 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: string = "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: number, max: number): number {
|
|
return Math.floor(Math.random() * (max - min) + min);
|
|
} |