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
**This project is currently in progress, feel free to [contribute](https://github.com/Simonwep/java-express/graphs/contributors) / [report](https://github.com/Simonwep/java-express/issues) issues! :)**
10
9
10
+
**[0.0.7-alpha](https://github.com/Simonwep/java-express/releases/tag/0.0.7) is ready, check it out!**
11
+
11
12
```java
12
13
Express app =newExpress();
13
14
@@ -25,21 +26,22 @@ Express app = new Express(Utils.getYourIp());
25
26
```
26
27
Default is localhost, so you can access, without setting the hostname, only from your local pc.
app.get(String context, HttpRequest handler); // Add an GET request handler
209
+
app.post(String context, HttpRequest handler); // Add an POST request handler
210
+
app.patch(String context, HttpRequest handler); // Add an PATCH request handler
211
+
app.put(String context, HttpRequest handler); // Add an PUT request handler
212
+
app.delete(String context, HttpRequest handler); // Add an DELETE request handler
213
+
app.all(HttpRequest handler); // Add an handler for all methods and contexts
214
+
app.all(String context, HttpRequest handler); // Add an handler for all methods but for an specific context
215
+
app.all(String context, String method, HttpRequest handler); // Add an handler for an specific method and context
216
+
app.use(String context, String method, HttpRequest handler); // Add an middleware for an specific method and context
217
+
app.use(HttpRequest handler); // Add an middleware for all methods but for an specific context
218
+
app.use(String context, HttpRequest handler); // Add an middleware for all methods and contexts
219
+
app.use(String context, ExpressRouter router); // Add an router for an specific root context
220
+
app.use(ExpressRouter router); // Add an router for the root context (/)
221
+
app.onParam(String name, HttpRequest handler); // Add an listener for an specific url parameter
222
+
app.getParameterListener(); // Returns all parameterlistener
223
+
app.get(String key); // Get an environment variable
224
+
app.set(String key, String val); // Set an environment variable
225
+
app.isSecure(); // Check if the server uses HTTPS
226
+
app.setExecutor(Executor executor); // Set an executor service for the request
227
+
app.listen(); // Start the async server on port 80
228
+
app.listen(ExpressListener onstart); // Start the async server on port 80, call the listener after starting
229
+
app.listen(int port); // Start the async server on an specific port
230
+
app.listen(ExpressListener onstart, int port); // Start the async server on an specific port call the listener after starting
231
+
app.stop(); // Stop the server and all middleware worker
232
+
```
201
233
202
234
## Response Object
203
235
Over the response object, you have serveral possibility like setting cookies, send an file and more. Below is an short explanation what methods exists:
236
+
(We assume that `res` is the `Response` object)
237
+
204
238
```java
205
-
app.get("/res", (req, res) -> {
206
-
// res.send(); // Send empty response
207
-
// res.send("Hello World"); // Send an string
208
-
// res.send("chart.pdf"); // Send an file
209
-
// setContentType(MediaType._txt); // Set the content type, default is txt/plain
210
-
// getContentType(); // Returns the current content type
211
-
// res.setStatus(Status._200); // Set the response status
212
-
// res.getStatus(); // Returns the current response status
213
-
// res.setCookie(new Cookie(...)); // Send an cookie
214
-
// res.isClosed(); // Check if already something has been send to the client
215
-
});
239
+
res.getContentType(); // Returns the current content type
240
+
res.setContentType(MediaType type); // Set the content type with enum help
241
+
res.setContentType(String type); // Set the content type
242
+
res.isClosed(); // Check if the response is already closed
243
+
res.getHeader(String key); // Get the value from an header field via key
244
+
res.setHeader(String key, String val); // Add an specific response header
245
+
res.send(String str); // Send an string as response
246
+
res.send(Path path); // Send an file as response
247
+
res.send(); // Send empty response
248
+
res.redirect(String location); // Redirect the request to another url
249
+
res.setCookie(Cookie cookie); // Add an cookie to the response
250
+
res.sendStatus(Status status); // Set the response status and send an empty response
251
+
res.getStatus(); // Returns the current status
252
+
res.setStatus(Status status); // Set the repose status
216
253
```
217
254
The response object calls are comments because **you can only call the .send(xy) once each request!**
218
255
219
256
## Request Object
220
-
With the request object you receive serveral data from the client which can be easily parsed by the given functions:
257
+
Over the `Request` Object you have access to serveral request stuff (We assume that `req` is the `Request` object):
// req.getContentType(); // Request content description, is here null because it's an GET request
227
-
// req.getBody(); // Request body inputstream
228
-
// req.getUserAgent(); // Request user-agent
229
-
// req.getParam("parameter"); // Returns an url parameter
230
-
// req.getQuery("queryname"); // Returns an url query by key
231
-
// req.getFormQuery("formqueryname"); // Returns an form input value
232
-
// req.getFormQuerys(); // Returns all form querys
233
-
// req.getCookie("user"); // Returns an cookie by name
234
-
// req.getCookies(); // Returns all cookies
235
-
// req.hasAuthorization(); // Check if the request contains an authorization header
236
-
// req.getAuthorization(); // Returns the authorization header
237
-
// req.getMiddlewareContent("name"); // Returns data from middleware
238
-
// req.pipe(new OutputStream() {...}); // Pipe the body to an outputstream
239
-
});
260
+
req.getAddress(); // Returns the INET-Adress from the client
261
+
req.getMethod(); // Returns the request method
262
+
req.getPath(); // Returns the request path
263
+
req.getQuery(String name); // Returns the query value by name
264
+
req.getHost(); // Returns the request host
265
+
req.getContentLength(); // Returns the content length
266
+
req.getContentType(); // Returns the content type
267
+
req.getMiddlewareContent(String name); // Returns the content from an middleware by name
268
+
req.getFormQuerys(); // Returns all form querys
269
+
req.getParams(); // Returns all params
270
+
req.getQuerys(); // Returns all querys
271
+
req.getFormQuery(String name); // Returns the form value by name
272
+
req.getHeader(String key); // Returns the value from an header field by name
273
+
req.getParam(String key); // Returns the url parameter by name
274
+
req.getApp(); // Returns the related express app
275
+
req.getCookie(String name); // Returns an cookie by his name
276
+
req.getCookies(); // Returns all cookies
277
+
req.getIp(); // Returns the client IP-Address
278
+
req.getUserAgent(); // Returns the client user agent
279
+
req.getURI(); // Returns the request URI
280
+
req.getAuthorization(); // Returns the request authorization
281
+
req.hasAuthorization(); // Check if the request has an authorization
282
+
req.pipe(OutputStream stream, int buffersize); // Pipe the request body to an outputstream
283
+
req.pipe(Path path, int buffersize); // Pipe the request body to an file
284
+
req.getBody(); // Returns the request inputstream
240
285
```
241
286
242
287
# Middleware
@@ -251,21 +296,21 @@ For example an middleware for all [request-methods](https://developer.mozilla.or
251
296
```java
252
297
// Global context, matches every request.
253
298
app.use((req, res) -> {
254
-
//Handle data
299
+
Handle data
255
300
});
256
301
```
257
302
You can also filter by [request-methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) and contexts:
258
303
```java
259
304
// Global context, you can also pass an context if you want
260
305
app.use("/home", "POST", (req, res) -> {
261
-
//Handle request by context '/home' and method 'POST'
306
+
Handle request by context '/home' and method 'POST'
262
307
});
263
308
```
264
309
In addition to that yo can use `*` which stands for every **context** or **request method**:
265
310
```java
266
311
// Global context, you can also pass an context if you want
267
312
app.use("/home", "*", (req, res) -> {
268
-
//Handle request which matches the context '/home' and all methods.
313
+
Handle request which matches the context '/home' and all methods.
269
314
});
270
315
```
271
316
## Create own middleware
@@ -312,24 +357,39 @@ No we can, as we learned above, include it with:
312
357
app.use(newPortMiddleware());
313
358
```
314
359
## Existing Middlewares
315
-
There are already some basic middlewares included, you can access these via static methods provided from `Express`.
360
+
There are already some basic middlewares included, you can access these via static methods provided from `Middleware`.
316
361
317
-
#### Static File serving
318
-
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.
362
+
#### Provide static Files
363
+
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/Middleware.java) middleware. But you can also provide other files like mp4 etc.
0 commit comments