|
9 | 9 |
|
10 | 10 | public class Examples { |
11 | 11 |
|
12 | | - public static void main(String[] args) throws IOException { |
13 | | - Express app = new Express(); |
14 | | - |
15 | | - app.get("/", (req, res) -> res.send("Hello World")) |
16 | | - |
17 | | - .get("/posts", (req, res) -> { |
18 | | - String page = req.getQuery("page"); // Contains '12' |
19 | | - String from = req.getQuery("from"); // Contains 'John' |
20 | | - res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John" |
21 | | - }) |
22 | | - |
23 | | - .get("/posts/:user/:type", (req, res) -> { |
24 | | - String user = req.getParam("user"); // Contains 'john' |
25 | | - String type = req.getParam("type"); // Contains 'all' |
26 | | - res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all" |
27 | | - }) |
28 | | - |
29 | | - .get("/setcookie", (req, res) -> { |
30 | | - Cookie cookie = new Cookie("username", "john"); |
31 | | - res.setCookie(cookie); |
32 | | - res.send("Cookie has been set!"); |
33 | | - }) |
34 | | - |
35 | | - .get("/showcookie", (req, res) -> { |
36 | | - Cookie cookie = req.getCookie("username"); |
37 | | - String username = cookie.getValue(); |
38 | | - res.send("The username is: " + username); // Prints "The username is: john" |
39 | | - }) |
40 | | - |
41 | | - .post("/register", (req, res) -> { |
42 | | - String email = req.getFormQuery("email"); |
43 | | - String username = req.getFormQuery("username"); |
44 | | - // Process data |
45 | | - |
46 | | - // Prints "E-Mail: [email protected], Username: john" |
47 | | - res.send("E-Mail: " + email + ", Username: " + username); |
48 | | - }) |
49 | | - |
50 | | - .get("/res", (req, res) -> { |
51 | | - // res.send(); // Send empty response |
52 | | - // res.send("Hello World"); // Send an string |
53 | | - // res.send("chart.pdf"); // Send an file |
54 | | - // res.setStatus(200); // Set the response status |
55 | | - // res.getStatus(); // Returns the current response status |
56 | | - // res.setCookie(new Cookie(...)); // Send an cookie |
57 | | - // res.isClosed(); // Check if already something has been send to the client |
58 | | - }) |
59 | | - |
60 | | - .get("/req/", (req, res) -> { |
61 | | - // req.getURI(); // Request URI |
62 | | - // req.getHost(); // Request host (mostly localhost) |
63 | | - // req.getMethod(); // Request method (here GET) |
64 | | - // req.getContentType(); // Request content type, is here null because it's an GET request |
65 | | - // req.getBody(); // Request body inputstream |
66 | | - // req.getUserAgent(); // Request user-agent |
67 | | - // req.getParam("parameter"); // Returns an url parameter |
68 | | - // req.getQuery("queryname"); // Returns an url query by key |
69 | | - // req.getFormQuery("formqueryname"); // Returns an form input value |
70 | | - // req.getFormQuerys(); // Returns all form querys |
71 | | - // req.getCookie("user"); // Returns an cookie by name |
72 | | - // req.getCookies(); // Returns all cookies |
73 | | - // req.hasAuthorization(); // Check if the request contains an authorization header |
74 | | - // req.getAuthorization(); // Returns the authorization header |
75 | | - // req.getMiddlewareContent("name"); // Returns data from middleware |
76 | | - // req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream |
77 | | - }) |
78 | | - |
79 | | - .use(Middleware.cookieSession("f3v4", 9000)) |
80 | | - |
81 | | - .get("/session", (req, res) -> { |
82 | | - |
83 | | - /* |
84 | | - * CookieSession named its data "SessionCookie" which is |
85 | | - * an SessionCookie so we can Cast it. |
86 | | - */ |
87 | | - SessionCookie sessionCookie = (SessionCookie) req.getMiddlewareContent("SessionCookie"); |
88 | | - int count; |
89 | | - |
90 | | - // Check if the data is null, we want to implement an simple counter |
91 | | - if (sessionCookie.getData() == null) { |
92 | | - |
93 | | - // Set the default data to 1 (first request with this session cookie) |
94 | | - count = (int) sessionCookie.setData(1); |
95 | | - } else { |
96 | | - |
97 | | - // Now we know that the cookie has an integer as data property, increase it |
98 | | - count = (int) sessionCookie.setData((int) sessionCookie.getData() + 1); |
99 | | - } |
100 | | - |
101 | | - // Send an info message |
102 | | - res.send("You take use of your session cookie " + count + " times."); |
103 | | - }) |
104 | | - |
105 | | - .listen(() -> System.out.println("Express is listening!")); |
106 | | - } |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + Express app = new Express(); |
| 14 | + |
| 15 | + app.get("/", (req, res) -> res.send("Hello World")) |
| 16 | + |
| 17 | + .get("/posts", (req, res) -> { |
| 18 | + String page = req.getQuery("page"); // Contains '12' |
| 19 | + String from = req.getQuery("from"); // Contains 'John' |
| 20 | + res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John" |
| 21 | + }) |
| 22 | + |
| 23 | + .get("/posts/:user/:type", (req, res) -> { |
| 24 | + String user = req.getParam("user"); // Contains 'john' |
| 25 | + String type = req.getParam("type"); // Contains 'all' |
| 26 | + res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all" |
| 27 | + }) |
| 28 | + |
| 29 | + .get("/setcookie", (req, res) -> { |
| 30 | + Cookie cookie = new Cookie("username", "john"); |
| 31 | + res.setCookie(cookie); |
| 32 | + res.send("Cookie has been set!"); |
| 33 | + }) |
| 34 | + |
| 35 | + .get("/showcookie", (req, res) -> { |
| 36 | + Cookie cookie = req.getCookie("username"); |
| 37 | + String username = cookie.getValue(); |
| 38 | + res.send("The username is: " + username); // Prints "The username is: john" |
| 39 | + }) |
| 40 | + |
| 41 | + .post("/register", (req, res) -> { |
| 42 | + String email = req.getFormQuery("email"); |
| 43 | + String username = req.getFormQuery("username"); |
| 44 | + // Process data |
| 45 | + |
| 46 | + // Prints "E-Mail: [email protected], Username: john" |
| 47 | + res.send("E-Mail: " + email + ", Username: " + username); |
| 48 | + }) |
| 49 | + |
| 50 | + .get("/res", (req, res) -> { |
| 51 | + // res.send(); // Send empty response |
| 52 | + // res.send("Hello World"); // Send an string |
| 53 | + // res.send("chart.pdf"); // Send an file |
| 54 | + // res.setStatus(200); // Set the response status |
| 55 | + // res.getStatus(); // Returns the current response status |
| 56 | + // res.setCookie(new Cookie(...)); // Send an cookie |
| 57 | + // res.isClosed(); // Check if already something has been send to the client |
| 58 | + }) |
| 59 | + |
| 60 | + .get("/req/", (req, res) -> { |
| 61 | + // req.getURI(); // Request URI |
| 62 | + // req.getHost(); // Request host (mostly localhost) |
| 63 | + // req.getMethod(); // Request method (here GET) |
| 64 | + // req.getContentType(); // Request content type, is here null because it's an GET request |
| 65 | + // req.getBody(); // Request body inputstream |
| 66 | + // req.getUserAgent(); // Request user-agent |
| 67 | + // req.getParam("parameter"); // Returns an url parameter |
| 68 | + // req.getQuery("queryname"); // Returns an url query by key |
| 69 | + // req.getFormQuery("formqueryname"); // Returns an form input value |
| 70 | + // req.getFormQuerys(); // Returns all form querys |
| 71 | + // req.getCookie("user"); // Returns an cookie by name |
| 72 | + // req.getCookies(); // Returns all cookies |
| 73 | + // req.hasAuthorization(); // Check if the request contains an authorization header |
| 74 | + // req.getAuthorization(); // Returns the authorization header |
| 75 | + // req.getMiddlewareContent("name"); // Returns data from middleware |
| 76 | + // req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream |
| 77 | + }) |
| 78 | + |
| 79 | + .use(Middleware.cookieSession("f3v4", 9000)) |
| 80 | + |
| 81 | + .get("/session", (req, res) -> { |
| 82 | + |
| 83 | + /* |
| 84 | + * CookieSession named its data "SessionCookie" which is |
| 85 | + * an SessionCookie so we can Cast it. |
| 86 | + */ |
| 87 | + SessionCookie sessionCookie = (SessionCookie) req.getMiddlewareContent("SessionCookie"); |
| 88 | + int count; |
| 89 | + |
| 90 | + // Check if the data is null, we want to implement an simple counter |
| 91 | + if (sessionCookie.getData() == null) { |
| 92 | + |
| 93 | + // Set the default data to 1 (first request with this session cookie) |
| 94 | + count = (int) sessionCookie.setData(1); |
| 95 | + } else { |
| 96 | + |
| 97 | + // Now we know that the cookie has an integer as data property, increase it |
| 98 | + count = (int) sessionCookie.setData((int) sessionCookie.getData() + 1); |
| 99 | + } |
| 100 | + |
| 101 | + // Send an info message |
| 102 | + res.send("You take use of your session cookie " + count + " times."); |
| 103 | + }) |
| 104 | + |
| 105 | + .listen(() -> System.out.println("Express is listening!")); |
| 106 | + } |
107 | 107 |
|
108 | 108 | } |
0 commit comments