express.ts 1.77 KB
Newer Older
Tung Vu's avatar
Tung Vu committed
1 2 3 4 5 6 7
import {Request, Response} from "express";
import express = require("express");
import bodyParser = require("body-parser");

let router = express();
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended: true}));
Tung Vu's avatar
Tung Vu committed
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
router.listen(8080);

console.log("Gestartet: http://localhost:8080/raten/1");
console.log("Gestartet: http://localhost:8080/cheat/1");
console.log("Gestartet: http://localhost:8080/reset");

function random(): Number {
    let min = 1;
    let max = 10;
    return Math.floor(Math.random() * (max - min)) + min;
}

let ziel: Number = random();
let password: String = "123123";

router.get("/raten/:zahl", function (req: Request, res: Response) {
    let geraten = req.params["zahl"];

    if (isNaN(geraten)) {
        res.json({antwort: "Fehler!"});
    }
    else if (geraten > ziel) {
        res.json({antwort: "Kleiner..."});
    }
    else if (geraten < ziel) {
        res.json({antwort: "Größer..."});
    }
    else {
        res.json({antwort: "Richtig!"});
    }
});

router.get("/cheat/:password", function (req: Request, res: Response) {
    if(req.params["password"] == password) {
        res.json({antwort: ziel});
    }
    else {
        res.json({antwort: "Passwort abgelehnt!"});
    }
Tung Vu's avatar
Tung Vu committed
47 48
});

Tung Vu's avatar
Tung Vu committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
router.get("/reset", function (req: Request, res: Response) {
    ziel = random();
    res.json({antwort: "Neue Zahl."});
});

router.post("/set", function (req: Request, res: Response) {
    let newNumber = req.body["new"];
    let enteredPassword = req.body["password"];

    if(enteredPassword != password) {
        res.json({antwort: "Passwort abgelehnt!"});
        return;
    }

    if(isNaN(newNumber) || newNumber == "") {
        res.json({antwort: "Das ist keine Zahl!"});
        return;
    }


    ziel = newNumber;
    res.json({antwort: "okay"});
});
Tung Vu's avatar
Tung Vu committed
72