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

Tung Vu's avatar
Tung Vu committed
5 6 7
let router = express();
router.use(bodyParser.json());
router.listen(8080, () => {
Tung Vu's avatar
Tung Vu committed
8 9
  console.log("Gestartet!");
  console.log("Aufrufbar sollten sein: ");
Tung Vu's avatar
Tung Vu committed
10 11 12 13 14 15
  console.log("  http://localhost:8080/name");
  console.log("  http://localhost:8080/htmlfile");
  console.log("  http://localhost:8080/gauss");
  console.log("  http://localhost:8080/liste");
  console.log("  http://localhost:8080/object");
});
Tung Vu's avatar
Tung Vu committed
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 47 48 49 50 51 52 53 54
router.get("/name", function(req: Request, res: Response){
    res.json({vorname: "Max", nachname: "Mahr"});
});

router.use("/htmlfile", express.static(__dirname + '/name.html'));

router.get("/gauss", function (req:Request, res: Response){
  let a: number = 0;
  for (let i=1; i>=100; i++){
      a += i
}

res.json({ergebnis: a});
});

router.get("/liste", function (req: Request, res: Response) {
    let Zahlen: number[] = [];

    for (let i = 1; i <= 100; i++) {
        Zahlen.push(i)
    }
    res.json({ergebnis: Zahlen});
});

class Person {
    public vorname: string;
    public nachname: string;

    constructor(_vorname: string, _nachname: string) {
        this.vorname = _vorname;
        this.nachname = _nachname;
    }
}

router.get("/object", function (req: Request, res: Response) {
    let myObject = new Person("Max", "Mahr");
    res.json(myObject);
});

Tung Vu's avatar
Tung Vu committed
55 56