image/svg+xml

Hop.js

version 3.6.0 16 February 2024

.keep

Hop.js: a multitier JavaScript

Hop.js (aka Hop) is:

Note: Hop also supports the Scheme programming language. With Hop.js, JavaScript and Scheme are fully interoperable and applications can mix both languages. This page mostly describes the JavaScript layer. The Scheme layer is described in a dedicated programming manual.

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 and execute:

$ hopjs -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</div></html>;
}

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

service hello({ name: who }) {
  return <html><div onclick=~{ alert("Hi " + ${who} + "!") }>hello</div></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);

user.add({ name: "hopjs",
           password: user.encryptPassword("hopjs", "inria"),
           services: "*",
           directories: "*"
         });
user.add({ name: "anonymous",
           services: ["wizard"],
           directories: hop.loadPath
         });	  
EOF

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