Skip to content

Commit

Permalink
Update README / polish files
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Feb 15, 2018
1 parent 243a28d commit c14118d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 7 deletions.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


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


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

Quick reference:
Docs:
* [Routing](#routing)
* [Direct](#direct)
* [With Router](#with-router)
* [URL Basics](#url-basics)
* [URL Parameter](#url-parameter)
* [URL Parameter Listener](#url-parameter-listener)
Expand All @@ -39,6 +43,64 @@ Quick reference:
* [License](#license)

Every following code can be also found in [this package](https://github.com/Simonwep/java-express/tree/master/src/examples).

# Routing
## Direct
You can add routes (And middlewares) directly to the Express object to handle requests:
```java
Express app = new Express();

// Sample for home routes
app.get("/", (req, res) -> res.send("Hello index!"));
app.get("/home", (req, res) -> res.send("Homepage"));
app.get("/about", (req, res) -> res.send("About"));

// Sample for user
app.get("/user/login", (req, res) -> res.send("Please login!"));
app.get("/user/register", (req, res) -> res.send("Join now!"));

app.listen();
```
It also directly supports directly methods like `POST` `PATCH` `DELETE` and `PUT` others need to be created manually:
```java
Express app = new Express();

// Basic methods
app.get("/user", (req, res) -> res.send("Get an user!"));
app.patch("/user", (req, res) -> res.send("Modify an user!"));
app.delete("/user", (req, res) -> res.send("Delete an user!"));
app.put("/user", (req, res) -> res.send("Add an user!"));

// Example fot the CONNECT method
app.on("/user", "CONNECT", (req, res) -> res.send("Connect!"));

app.listen();
```

## With Router
But it's better to split your code, right? With the `ExpressRouter` you can create routes and add it later to the `Express` object:
```java
Express app = new Express();

// Define router for index sites
ExpressRouter indexRouter = new ExpressRouter();
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
indexRouter.get("/index", (req, res) -> res.send("Index"));
indexRouter.get("/about", (req, res) -> res.send("About"));

// Define router for user pages
ExpressRouter userRouter = new ExpressRouter();
userRouter.get("/user/login", (req, res) -> res.send("User Login"));
userRouter.get("/user/register", (req, res) -> res.send("User Register"));
userRouter.get("/user/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));

// Add roter
app.use(indexRouter);
app.use(userRouter);

app.listen();
```

# URL Basics
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:
```java
Expand Down
12 changes: 6 additions & 6 deletions src/examples/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public static void main(String[] args) throws IOException {
res.send("Hello World");
});

app.get("/posts/:user/:type", (req, res) -> {
String user = req.getParam("user"); // Contains 'john'
String type = req.getParam("type"); // Contains 'all'
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
});

app.get("/posts", (req, res) -> {
String page = req.getQuery("page"); // Contains '12'
String from = req.getQuery("from"); // Contains 'John'
res.send("Page: " + page + ", from: " + from); // Send: "Page: 12, from: John"
});

app.get("/posts/:user/:type", (req, res) -> {
String user = req.getParam("user"); // Contains 'john'
String type = req.getParam("type"); // Contains 'all'
res.send("User: " + user + ", type: " + type); // Send: "User: john, type: all"
});

app.get("/setcookie", (req, res) -> {
Cookie cookie = new Cookie("username", "john");
res.setCookie(cookie);
Expand Down
31 changes: 31 additions & 0 deletions src/examples/Routing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package examples;

import express.Express;
import express.ExpressRouter;

public class Routing {

public static void main(String[] args) throws Throwable {
Express app = new Express();

// Define router for index sites
ExpressRouter indexRouter = new ExpressRouter();
indexRouter.get("/", (req, res) -> res.send("Hello World!"));
indexRouter.get("/index", (req, res) -> res.send("Index"));
indexRouter.get("/about", (req, res) -> res.send("About"));

// Define router for user pages
ExpressRouter userRouter = new ExpressRouter();
userRouter.get("/user/login", (req, res) -> res.send("User Login"));
userRouter.get("/user/register", (req, res) -> res.send("User Register"));
userRouter.get("/user/:username", (req, res) -> res.send("You want to see: " + req.getParam("username")));

// Add roter
app.use(indexRouter);
app.use(userRouter);

// Start server
app.listen();
}

}
1 change: 1 addition & 0 deletions src/express/ExpressListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/**
* @author Simon Reinisch
* Listener for express actions
*/
public interface ExpressListener {
void action();
Expand Down
4 changes: 4 additions & 0 deletions src/express/ExpressRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.util.ArrayList;

/**
* @author Simon Reinisch
* Basic implementation of an router
*/
public class ExpressRouter implements Router {

private final ArrayList<FilterWorker> WORKER;
Expand Down
4 changes: 4 additions & 0 deletions src/express/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.sun.istack.internal.NotNull;
import express.http.HttpRequest;

/**
* @author Simon Reinisch
* Router interface for express
*/
public interface Router {

/**
Expand Down
4 changes: 4 additions & 0 deletions src/express/http/CookieFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.time.Instant;
import java.util.HashMap;

/**
* @author Simon Reinisch
* CookieFactory to parse cookies
*/
public class CookieFactory {

/**
Expand Down

0 comments on commit c14118d

Please sign in to comment.