Skip to content

Commit fe4504e

Browse files
committed
Small bug fix, add host-binding
1 parent 80bcfab commit fe4504e

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/express/Express.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,25 @@ public class Express {
2626
private final ExpressFilterChain MIDDLEWARE_CHAIN = new ExpressFilterChain();
2727
private final ExpressFilterChain FILTER_CHAIN = new ExpressFilterChain();
2828

29+
private String hostname = "localhost";
2930
private HttpServer httpServer;
3031

32+
/**
33+
* Create an express instance and bind the server to an hostname.
34+
* Default is "Localhost"
35+
*
36+
* @param hostname The host name
37+
*/
38+
public Express(String hostname) {
39+
if (hostname != null)
40+
this.hostname = hostname;
41+
}
42+
43+
/**
44+
* Default, will bind the server to "localhost"
45+
*/
46+
public Express() {}
47+
3148
/**
3249
* Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
3350
*
@@ -175,7 +192,7 @@ public void listen(Action onStart, int port) throws IOException {
175192

176193

177194
// Create http server
178-
httpServer = HttpServer.create(new InetSocketAddress("localhost", port), 0);
195+
httpServer = HttpServer.create(new InetSocketAddress(this.hostname, port), 0);
179196
httpServer.setExecutor(null);
180197

181198
httpServer.createContext("/", httpExchange -> {
@@ -190,7 +207,7 @@ public void listen(Action onStart, int port) throws IOException {
190207
httpServer.start();
191208

192209
// Fire listener
193-
onStart.action();
210+
if (onStart != null) onStart.action();
194211
} catch (IOException e) {
195212
// TODO: Handle errors
196213
e.printStackTrace();

0 commit comments

Comments
 (0)