HttpServer server = new HttpServer(80);
HttpServer server = new HttpServer(new InetSocketAddress(80), 0);
server.setExecutor(Executors.newSingleThreadExecutor());
server.setExecutor(Executors.newFixedThreadPool(10));
This is the default executor.
server.setExecutor(Executors.newCachedThreadPool());
server.get("/home", (req, res) -> {
});
server.post("/home", (req, res) -> {
});
server.handle("*", "/home", (req, res) -> {
});
server.addHandler("/home", (req, res) -> {
});
req.getRequestHeader("Connection");
req.getRequestURI();
req.getRequestMethod();
req.getRemoteAddress();
Cookie[] cookies = req.getCookies();
Cookie cookie = req.getCookie("name");
String param = req.getParam("name");
String body = req.getRequestBody();
byte[] body = req.getRequestBodyBytes();
res.status(200)
res.writeHead(200, (head) -> {
head.setHeader("Content-Type", "text/plain");
});
res.send("Hello World!");
Or with status code
res.send(200, "Hello World!");
You can also use write. This will not end the response and you can send bytes
res.write("Hello World!");
Using send will already end the response.
res.end();
server.start();
Or
server.start(()->{
System.out.println("Server started!");
});