version 3.0.0-rc9

Hop: a multitier JavaScript

Hop is:

Hop programs execute in the context of a builtin web server. They define services, which are super JavaScript functions that get automatically invoked when HTTP requests are received. Functions and services are almost syntactically similar but the latter are defined using the service keyword:

service hello() {
  return "hello world";

To run this program put this code in the file hello.js and execute:

hop -p 8080 hello.js

You can now browse http://localhost:8080/hop/hello.

Hop extends JavaScript with the geniune HTML. if we want to modify our service to make it return an HTML document, we can use:

service hello() {
  return <html><div>hello world</div></html>;

Hop is multitier. That is client-side codes are also implemented in Hop. The ~{ mark switches from server-side context to client-side context:

service hello() {
  return <html><div onclick=~{ alert( "world" ) }>hello</html>;

Hop client-side code and server-side can also be mixed using the ${ mark:

service hello( { who: "foo" } ) {
  return <html><div onclick=~{ alert( "Hi " + ${who} + "!") }>hello</html>;

By default Hop.js only accepts to serve authenticated requests. Before executing any programs users must be declared. These declarations go into the $HOME/.config/hop/hoprc.js file. The following declare a user named hopjs whose password is inria and that is allowed to execute any Hop.js service, the declaration services: "*", and download any file readable from the server process, the declaration directories: "*":

mkdir -p $HOME/.config/hop && cat > $HOME/.config/hop/hoprc.js << EOF
hop = require( "hop" );
var user = require( hop.user );
var config = require( hop.config );

user.add( { name: "hopjs",
            password: user.encryptPassword( "hopjs", "inria" ),
            services: "*",
            directories: "*"
          } );

You are now ready to execute Hop.js programs! Many more additional examples can be found in the source development tree.