|
| 1 | + |
1 | 2 |  |
2 | 3 |
|
3 | 4 |
|
@@ -39,6 +40,16 @@ Quick reference: |
39 | 40 |
|
40 | 41 | Every following code can be also found in [this package](https://github.com/Simonwep/java-express/tree/master/src/examples). |
41 | 42 | # URL Basics |
| 43 | +Over the express object you can create handler for all [request-methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) and contexts. Some examples: |
| 44 | +```java |
| 45 | +app.get("/home", (req, res) -> { |
| 46 | + // Will match every request which uses the 'GET' method and matches the '/home' path |
| 47 | +}); |
| 48 | + |
| 49 | +app.post("/login", (req, res) -> { |
| 50 | + // Will match every request which uses the 'POST' method and matches the /login' path |
| 51 | +}); |
| 52 | +``` |
42 | 53 |
|
43 | 54 | ## URL Parameter |
44 | 55 | Sometime you want to create dynamic URL where some parts of the URL's are not static. |
@@ -172,17 +183,27 @@ Middleware are one of the most important functions of JavaExpress, with middlewa |
172 | 183 | * `ExpressFilter` - Is **required** to put data on the request listener. |
173 | 184 | * `ExpressFilterTask` - Can be used for middleware which needs an background thread. |
174 | 185 |
|
175 | | -To use an middleware, you simply call: |
| 186 | +Middlewares work, for you, exact same as request handler. |
| 187 | +For example an middleware for all [request-methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) and contexts: |
| 188 | + |
| 189 | +```java |
| 190 | +// Global context, matches every request. |
| 191 | +app.use((req, res) -> { |
| 192 | + // Handle data |
| 193 | +}); |
| 194 | +``` |
| 195 | +You can also filter by [request-methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) and contexts: |
176 | 196 | ```java |
177 | 197 | // Global context, you can also pass an context if you want |
178 | | -app.use(new YourMiddleWare()); |
| 198 | +app.use("/home", "POST", (req, res) -> { |
| 199 | + // Handle request by context '/home' and method 'POST' |
| 200 | +}); |
179 | 201 | ``` |
180 | | - |
181 | | -Or create an inline-middleware: |
| 202 | +In addition to that yo can use `*` which stands for every **context** or **request method**: |
182 | 203 | ```java |
183 | 204 | // Global context, you can also pass an context if you want |
184 | | -app.use((req, res) -> { |
185 | | - // Handle data |
| 205 | +app.use("/home", "*", (req, res) -> { |
| 206 | + // Handle request which matches the context '/home' and all methods. |
186 | 207 | }); |
187 | 208 | ``` |
188 | 209 | ## Create own middleware |
@@ -278,4 +299,4 @@ app.get("/session", (req, res) -> { |
278 | 299 |
|
279 | 300 | # License |
280 | 301 |
|
281 | | -This project is licensed under the MIT License - see the [LICENSE.md](https://choosealicense.com/licenses/mit/) file for details |
| 302 | +This project is licensed under the MIT License - see the [LICENSE.md](https://choosealicense.com/licenses/mit/) file for details |
0 commit comments