You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Over the response object, you have serveral possibility like setting cookies, send an file and more. Below is an short explanation what methods exists:
108
111
```java
109
112
app.get("/res", (req, res) -> {
110
-
// res.send(); // Send empty response
111
-
// res.send("Hello World"); // Send an string
112
-
// res.send("chart.pdf"); // Send an file
113
-
// res.setStatus(200); // Set the response status
114
-
// res.getStatus(); // Returns the current response status
115
-
// res.setCookie(new Cookie(...)); // Send an cookie
116
-
// res.isClosed(); // Check if already something has been send to the client
113
+
// res.send(); // Send empty response
114
+
// res.send("Hello World"); // Send an string
115
+
// res.send("chart.pdf"); // Send an file
116
+
// res.setStatus(200); // Set the response status
117
+
// res.getStatus(); // Returns the current response status
118
+
// res.setCookie(new Cookie(...)); // Send an cookie
119
+
// res.isClosed(); // Check if already something has been send to the client
117
120
});
118
121
```
119
122
The response object calls are comments because **you can only call the .send(xy) once each request!**
@@ -122,22 +125,22 @@ The response object calls are comments because **you can only call the .send(xy)
122
125
With the request object you receive serveral data from the client which can be easily parsed by the given functions:
* From interface HttpRequest, to handle the request.
171
-
*/
172
-
@Override
173
-
publicvoidhandle(Requestreq, Responseres) {
174
-
175
-
// Get the port
176
-
int port = req.getURI().getPort();
177
-
178
-
// Add the port to the request middleware map
179
-
req.addMiddlewareContent(this, port);
180
-
181
-
/**
182
-
* After that you can use this middleware by call:
183
-
* app.use(new PortMiddleware());
184
-
*
185
-
* Than you can get the port with:
186
-
* int port = (Integer) app.getMiddlewareContent("PortParser");
187
-
*/
188
-
}
189
-
190
-
/**
191
-
* Defines the middleware.
192
-
*
193
-
* @return The middleware name.
194
-
*/
195
-
@Override
196
-
publicStringgetName() {
197
-
return"PortParser";
198
-
}
172
+
/**
173
+
* From interface HttpRequest, to handle the request.
174
+
*/
175
+
@Override
176
+
publicvoidhandle(Requestreq, Responseres) {
177
+
178
+
// Get the port
179
+
int port = req.getURI().getPort();
180
+
181
+
// Add the port to the request middleware map
182
+
req.addMiddlewareContent(this, port);
183
+
184
+
/**
185
+
* After that you can use this middleware by call:
186
+
* app.use(new PortMiddleware());
187
+
*
188
+
* Than you can get the port with:
189
+
* int port = (Integer) app.getMiddlewareContent("PortParser");
190
+
*/
191
+
}
192
+
193
+
/**
194
+
* Defines the middleware.
195
+
*
196
+
* @return The middleware name.
197
+
*/
198
+
@Override
199
+
publicStringgetName() {
200
+
return"PortParser";
201
+
}
199
202
}
200
203
```
201
204
No we can, as we learned above, include it with:
202
205
```java
203
206
// Global context, you can also pass an context if you want
204
207
app.use(newPortMiddleware());
205
208
```
209
+
## Existing Middlewares
210
+
There are already some basic middlewares included:
211
+
212
+
#### Static File serving
213
+
If you want to allocate some files, like js-librarys or css files you can use the [static](https://github.com/Simonwep/java-express/blob/master/src/express/middleware/Static.java) middleware. But you can also provide other files like mp4 etc.
214
+
Example:
215
+
```java
216
+
app.use(newStatic("examplepath\\test_statics"));
217
+
```
218
+
Now you can access every files in the `test_statics` over the root adress `\`.
219
+
#### Cookie Session
220
+
Java Express also includes an simple cookie-session middleware:
221
+
```java
222
+
// You should use an meaningless cookie name for serveral security reasons, here f3v4.
223
+
// Also you can specify the maximum age of the cookie from the creation date, here 3000.
224
+
app.use(newCookieSession("f3v4", 9000));
225
+
```
226
+
To use a session cookie we need to get the data from the middleware which is actually an `SessionCookie`:
227
+
```java
228
+
// Cookie session example
229
+
app.get("/session", (req, res) -> {
230
+
231
+
/**
232
+
* CookieSession named his data "Session Cookie" which is
0 commit comments