Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

javascript

Jooby is available in JavaScript via Nashorn.

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-lang-js</artifactId>
  <version>1.1.1</version>
</dependency>

usage

Create an app.js file in your project directory:

var app = jooby();

app.get('/', function () 'Hey Jooby!');

In your pom.xml, set the application.class property to org.jooby.JoobyJs like this:

<properties>
  <application.class>org.jooby.JoobyJs</application.class>
</properties>

The org.jooby.JoobyJs class will set up Nashorn and start Jooby.

jooby function

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 nashorn:mozilla_compat.js.

routes

Routes work as in Java, but it is worth mentioning the available alternatives when you 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 with access to the request object
  this.get('/', function (req) {
    var x = require(X)
    return x.doSomething();
  });

  // access to the request and the response (as in express.js)
  this.get('/', function (req, rsp) {
    rsp.send('Hey Jooby!');
  });

  // access to the request and the response
  this.get('/', function (req, rsp, chain) {
    chain.next();
  });

})();

running a javascript app

  • via maven: mvn jooby:run
  • java: java org.jooby.JoobyJs. The app.js file must be present in the app directory