Skip to content

Commit c14118d

Browse files
committed
Update README / polish files
1 parent 243a28d commit c14118d

File tree

7 files changed

+113
-7
lines changed

7 files changed

+113
-7
lines changed

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
![Java Express Logo](https://image.ibb.co/mCdxtm/java_express.png)
34

45

@@ -24,7 +25,10 @@ Express app = new Express(Utils.getYourIp());
2425
```
2526
Default is localhost, so you can access, without setting the hostname, only from your local pc.
2627

27-
Quick reference:
28+
Docs:
29+
* [Routing](#routing)
30+
* [Direct](#direct)
31+
* [With Router](#with-router)
2832
* [URL Basics](#url-basics)
2933
* [URL Parameter](#url-parameter)
3034
* [URL Parameter Listener](#url-parameter-listener)
@@ -39,6 +43,64 @@ Quick reference:
3943
* [License](#license)
4044

4145
Every following code can be also found in [this package](https://github.com/Simonwep/java-express/tree/master/src/examples).
46+
47+
# Routing
48+
## Direct
49+
You can add routes (And middlewares) directly to the Express object to handle requests:
50+
```java
51+
Express app = new Express();
52+
53+
// Sample for home routes
54+
app.get("/", (req, res) -> res.send("Hello index!"));
55+
app.get("/home", (req, res) -> res.send("Homepage"));
56+
app.get("/about", (req, res) -> res.send("About"));
57+
58+
// Sample for user
59+
app.get("/user/login", (req, res) -> res.send("Please login!"));
60+
app.get("/user/register", (req, res) -> res.send("Join now!"));
61+
62+
app.listen();
63+
```
64+
It also directly supports directly methods like `POST` `PATCH` `DELETE` and `PUT` others need to be created manually:
65+
```java
66+
Express app = new Express();
67+
68+
// Basic methods
69+
app.get("/user", (req, res) -> res.send("Get an user!"));
70+
app.patch("/user", (req, res) -> res.send("Modify an user!"));
71+
app.delete("/user", (req, res) -> res.send("Delete an user!"));
72+
app.put("/user", (req, res) -> res.send("Add an user!"));
73+
74+
// Example fot the CONNECT method
75+
app.on("/user", "CONNECT", (req, res) -> res.send("Connect!"));
76+
77+
app.listen();
78+
```
79+
80+
## With Router
81+
But it's better to split your code, right? With the `ExpressRouter` you can create routes and add it later to the `Express` object:
82+
```java
83+
Express app = new Express();
84+
85+
// Define router for index sites
86+
ExpressRouter indexRouter = new ExpressRouter();
87+
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
88+
indexRouter.get("/index", (req, res) -> res.send("Index"));
89+
indexRouter.get("/about", (req, res) -> res.send("About"));
90+
91+
// Define router for user pages
92+
ExpressRouter userRouter = new ExpressRouter();
93+
userRouter.get("/user/login", (req, res) -> res.send("User Login"));
94+
userRouter.get("/user/register", (req, res) -> res.send("User Register"));
95+
userRouter.get("/user/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));
96+
97+
// Add roter
98+
app.use(indexRouter);
99+
app.use(userRouter);
100+
101+
app.listen();
102+
```
103+
42104
# URL Basics
43105
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:
44106
```java

src/examples/Examples.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ public static void main(String[] args) throws IOException {
1515
res.send("Hello World");
1616
});
1717

18-
app.get("/posts/:user/:type", (req, res) -> {
19-
String user = req.getParam("user"); // Contains 'john'
20-
String type = req.getParam("type"); // Contains 'all'
21-
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
22-
});
23-
2418
app.get("/posts", (req, res) -> {
2519
String page = req.getQuery("page"); // Contains '12'
2620
String from = req.getQuery("from"); // Contains 'John'
2721
res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John"
2822
});
2923

24+
app.get("/posts/:user/:type", (req, res) -> {
25+
String user = req.getParam("user"); // Contains 'john'
26+
String type = req.getParam("type"); // Contains 'all'
27+
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
28+
});
29+
3030
app.get("/setcookie", (req, res) -> {
3131
Cookie cookie = new Cookie("username", "john");
3232
res.setCookie(cookie);

src/examples/Routing.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package examples;
2+
3+
import express.Express;
4+
import express.ExpressRouter;
5+
6+
public class Routing {
7+
8+
public static void main(String[] args) throws Throwable {
9+
Express app = new Express();
10+
11+
// Define router for index sites
12+
ExpressRouter indexRouter = new ExpressRouter();
13+
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
14+
indexRouter.get("/index", (req, res) -> res.send("Index"));
15+
indexRouter.get("/about", (req, res) -> res.send("About"));
16+
17+
// Define router for user pages
18+
ExpressRouter userRouter = new ExpressRouter();
19+
userRouter.get("/user/login", (req, res) -> res.send("User Login"));
20+
userRouter.get("/user/register", (req, res) -> res.send("User Register"));
21+
userRouter.get("/user/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));
22+
23+
// Add roter
24+
app.use(indexRouter);
25+
app.use(userRouter);
26+
27+
// Start server
28+
app.listen();
29+
}
30+
31+
}

src/express/ExpressListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* @author Simon Reinisch
5+
* Listener for express actions
56
*/
67
public interface ExpressListener {
78
void action();

src/express/ExpressRouter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import java.util.ArrayList;
1111

12+
/**
13+
* @author Simon Reinisch
14+
* Basic implementation of an router
15+
*/
1216
public class ExpressRouter implements Router {
1317

1418
private final ArrayList<FilterWorker> WORKER;

src/express/Router.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import com.sun.istack.internal.NotNull;
44
import express.http.HttpRequest;
55

6+
/**
7+
* @author Simon Reinisch
8+
* Router interface for express
9+
*/
610
public interface Router {
711

812
/**

src/express/http/CookieFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.time.Instant;
44
import java.util.HashMap;
55

6+
/**
7+
* @author Simon Reinisch
8+
* CookieFactory to parse cookies
9+
*/
610
public class CookieFactory {
711

812
/**

0 commit comments

Comments
 (0)