JExpress.java, a light and slow express.js clone written in Java 17 (in one file). It uses virtual threads if run with Java 21 (or Java 19/20 with --enable-preview).
There is also JExpress8.java, a version backward compatible with Java 8.
- JExpress: express(), get(path, callback), post(path, callback), put(path, callback), delete(path, callback), use(path, handler), listen(port) and staticFiles(root).
- Request: bodyArray(), bodyObject(), bodyText(), get(header), method(), param(name) and path().
- Response: status(status), type(type, charset), set(field, value), append(field, value), json(object), send(body) and sendFile(path).
The full javadoc
public static void main(String[] args) {
var app = express();
app.use(staticFiles(Path.of("public")));
app.get("/hello/:id", (req, res) -> {
var id = req.param("id");
record Hello(String id) {}
res.json(new Hello(id));
});
app.get("/LICENSE", (req, res) -> {
res.sendFile(Path.of("LICENSE"));
});
app.listen(3000);
}
-
Run the application with
cd src/main/java java JExpress.java
-
Test the application with Maven
mvn clean package