# Examples

# Hello World

Like every other library, we will start with an hello world example. The first step is to create a project:

{
  "name": "hello-es4x",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "@vertx/core": "latest",
    "@es4x/create": "latest"
  },
  "scripts": {
    "postinstall": "es4x install",
    "start": "es4x"
  }
}

# Install the required dependencies

$ npm i

> [email protected] postinstall .../hello-world
> es4x install

npm notice created a lockfile as package-lock.json. You should commit this file.
added 2 packages from 1 contributor and audited 2 packages in 6.704s
found 0 vulnerabilities

WARNING

If you see the message Installing GraalJS..., this means that your system wide java is not a GraalVM installation. This is perfectly OK as extra packages are downloaded to ensure the best performance.

WARNING

If you see the message Current JDK only supports GraalJS in Interpreted mode!, this means that your system wide java command version is either less than 11 or OpenJ9.

# Write the code

Now that the project is ready to be used, we can write the code:

/// <reference types="@vertx/core" />
// @ts-check

console.log('Hello ES4X');

# Run it

$ npm start

> [email protected] start .../hello-world
> es4x

Hello ES4X
Succeeded in deploying verticle

WARNING

If you see the message Current JDK only supports GraalJS in Interpreted mode!, this means that your system wide java command version is either less than 11 or OpenJ9.

# Web Application

In this example we will create a simple web application:

{
  "name": "web-application",
  "version": "0.0.1",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "@vertx/core": "latest",
    "@vertx/web": "latest"
  },
  "devDependencies": {
    "@es4x/create": "latest"
  },
  "scripts": {
    "start": "es4x",
    "postinstall": "es4x install"
  }
}

# Install the required dependencies

$ npm i

> [email protected] postinstall .../hello-world
> es4x install

npm notice created a lockfile as package-lock.json. You should commit this file.
added 2 packages from 1 contributor and audited 2 packages in 6.704s
found 0 vulnerabilities

WARNING

If you see the message Installing GraalJS..., this means that your system wide java is not a GraalVM installation. This is perfectly OK as extra packages are downloaded to ensure the best performance.

WARNING

If you see the message Current JDK only supports GraalJS in Interpreted mode!, this means that your system wide java command version is either less than 11 or OpenJ9.

# Write the code

Now that the project is ready to be used, we can write the code:

/// <reference types="@vertx/core" />
// @ts-check

import {Router} from '@vertx/web';

const app = Router.router(vertx);

// serve a simple Hello, World! text message
app.get("/plaintext").handler(ctx => {
  ctx.response()
    .putHeader("Content-Type", "text/plain")
    .end('Hello, World!');
});

// serve a simple Hello, World! JSON message
app.get("/json").handler(ctx => {
  ctx.response()
    .putHeader("Content-Type", "application/json")
    .end(JSON.stringify({message: 'Hello, World!'}));
});

// create an HTTP server and let it be handled by the application
vertx
  .createHttpServer()
  .requestHandler(app)
  .listen(8080);

console.log('Server listening at: http://localhost:8080/');

# Postgres Access

In this example we will create a simple Postgres query application:

{
  "name": "posgres",
  "version": "0.0.1",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "@vertx/core": "latest",
    "@vertx/pg-client": "latest"
  },
  "devDependencies": {
    "@es4x/create": "latest"
  },
  "scripts": {
    "start": "es4x",
    "postinstall": "es4x install"
  }
}

# Install the required dependencies

$ npm i

> [email protected] postinstall .../hello-world
> es4x install

npm notice created a lockfile as package-lock.json. You should commit this file.
added 2 packages from 1 contributor and audited 2 packages in 6.704s
found 0 vulnerabilities

WARNING

If you see the message Installing GraalJS..., this means that your system wide java is not a GraalVM installation. This is perfectly OK as extra packages are downloaded to ensure the best performance.

WARNING

If you see the message Current JDK only supports GraalJS in Interpreted mode!, this means that your system wide java command version is either less than 11 or OpenJ9.

# Write the code

Now that the project is ready to be used, we can write the code:

/// <reference types="@vertx/core" />
// @ts-check

import { PgPool } from '@vertx/pg-client';
import { PoolOptions } from '@vertx/sql-client/options';
import { PgConnectOptions } from '@vertx/pg-client/options';
import { Tuple } from '@vertx/sql-client';

const SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";

let connectOptions = new PgConnectOptions()
  .setCachePreparedStatements(true)
  .setHost('database-server')
  .setUser('dbuser')
  .setPassword('dbpass')
  .setDatabase('hello_world');

// Pool options
let poolOptions = new PoolOptions()
  .setMaxSize(1);

// Create the client pool
let client = PgPool.pool(vertx, connectOptions, poolOptions);

// select a random row from the database
client.preparedQuery(SELECT_WORLD).execute(Tuple.of(Math.random()), res => {
  if (res.succeeded()) {
    let resultSet = res.result().iterator();

    if (!resultSet.hasNext()) {
      ctx.fail(404);
      return;
    }

    let row = resultSet.next();

    console.log({
      id: row.getInteger(0),
      randomNumber: row.getInteger(1)
    });
  } else {
    console.trace(res.cause());
  }
});

# More examples?

If you would like to see more examples, just go to vertx-examples (opens new window). Even though the examples are written in Java, by following the advanced guide, you will see how the use of java APIs can be trivial.