Jooby is available in JavaScript via Nashorn.
var app = jooby();
app.get('/', function () 'Hey Jooby!');
The jooby
function always returns a new application:
var app = jooby();
or pass a function to jooby
:
jooby(function () {
this.get('/', function () 'Hey Jooby!');
})();
Another minor but useful feature is the: import of classes and packages when you go with the functional version:
jooby(function (Jackson) {
use(new Jackson());
this.get('/', function () 'Hey Jooby!');
})(org.jooby.json.Jackson);
Or import an entire package:
jooby(function (Jackson) {
use(new Jackson());
this.get('/', function () 'Hey Jooby!');
})(org.jooby.json);
Import of packages is done via: importPackage
function from in nashorn:mozilla_compat.js
.
Routes work as in Java, but it is worth to mention what are the available alternatives at the time you need to write a route in JavaScript:
jooby(function () {
// returns a constant value, using Function expression closures
this.get('/', function () 'Hey Jooby!');
// returns a constant value
this.get('/', function () {
return 'Hey Jooby!';
});
// returns a value but access to request object
this.get('/', function (req) {
var x = require(X)
return x.doSomething();
});
// access to request and rsp (like in express.js)
this.get('/', function (req, rsp) {
rsp.send('Hey Jooby!');
});
// access to request and rsp
this.get('/', function (req, rsp, chain) {
chain.next();
});
})();
- via maven:
mvn jooby:run
- java:
java org.jooby.Jooby
. Theapp.js
file must be present in the app directory