Vert.x 3 says “hello” to NPM users

In pro­gram­ming lit­er­a­ture it has be­come the stan­dard to cre­ate a hello world pro­gram as the first ex­am­ple. In this ar­ti­cle, I’ll be demon­strat­ing how NPM users can quickly get started with vert.x. You will see that it is not that dif­fer­ent and in fact it can be done using the tools you’re used to. Note that al­though we are using NPM we are not re­ly­ing on node.js, all javascript code runs on the JVM.

Hello World Examples

Here are four sim­ple hello world ex­am­ples. The com­ments in the code ex­plain how the code works and the text around it ex­plain what it does and how to test it.

Hello Console

This ex­am­ple is about as plain as it can get. It prints the words ”Hello World” to the ter­mi­nal. If you’re a javascript de­vel­oper you should be al­ready used to npm and know that you al­ways start a project with the file package.json:

{
  "name": "vertx3-hello-console",
  "private": true,
  "dependencies": {
    "vertx3-min": "3.0.0-1"
  },
  "scripts": {
    "start": "./node_modules/.bin/vertx run server.js"
  }
}

Note that we have a de­pen­dency wich is ob­vi­ous vert.x now note that there are 3 flavours of this de­pen­dency:

Ac­cord­ing to your needs you can pick a dif­fer­ent flavour, since for a sim­ple hello world we only need the min­i­mal that is the one we add to the de­pen­dency prop­erty.

Now we need to do a sim­ple hello app, we will call this file ”server.js“:

// Call the console.log function.
console.log("Hello World");

You can run this by ex­e­cut­ing:

npm install
npm start

The first com­mand re­trieve the vert.x stack while the sec­onds starts your pro­gram.

Hello HTTP

I’d guess that while it’s not the only use case for vert.x, most peo­ple are using it as a web ap­pli­ca­tion plat­form. So the next ex­am­ple will be a sim­ple HTTP server that re­sponds to every re­quest with the plain text mes­sage ”Hello Worldserver.js:

vertx.createHttpServer()
  .requestHandler(function (req) {
    req.response()
      .putHeader("content-type", "text/plain")
      .end("Hello World!");
}).listen(8080);

Now you can reuse the same package.json we’ve just de­fined in the pre­vi­ous sec­tion and start the server with npm start. Once the server starts you can open a browser to http://localhost:8080 and enjoy the mes­sage.

Hello TCP

Vert.x also makes an ex­cel­lent TCP server, and here is an ex­am­ple that re­sponds to all TCP con­nec­tions with the mes­sage “Hello World” and then closes the con­nec­tion server.js:

var server = vertx.createNetServer();
server.connectHandler(function (socket) {
  socket.write("Hello World!\n");
  socket.close();
});

server.listen(7000, "localhost");

Again reuse the pre­vi­ous package.json and test it by doing telnet localhost 7000.

Hello Web

Often you won’t be using vert.x built-​in li­braries be­cause they are de­signed to be very low level. This makes vert.x quick, nim­ble, and easy to main­tain, but if you are plan­ning to build a com­plex ap­pli­ca­tion you want some pro­duc­tiv­ity and rely on a sim­ple web frame­work. For this spe­cific case there is vert.x web, a sim­ple, yet pro­duc­tive frame­work, to build fast web ap­pli­ca­tion with rout­ing, tem­plate ren­der­ing, lots of mid­dle­ware etc…usu­ally not enough to get started on a real world ap­pli­ca­tion. This ex­am­ple shows an HTTP server that re­sponds with “Hello World” to all re­quests to ”/” and re­sponds with a 404 error to every­thing else server.js:

var Router = require("vertx-web-js/router");
var server = vertx.createHttpServer();

var router = Router.router(vertx);

router.get("/").handler(function (ctx) {
  // This handler will be called for "/" requests
  var response = ctx.response();
  response.putHeader("content-type", "text/plain");

  // Write to the response and end it
  response.end("Hello World!");
});

server.requestHandler(router.accept).listen(8080);

In order to test this, you will need to in­stall the vertx3-full stack. There are two ways to do this. You can ei­ther in­stall it glob­ally npm install -g vertx3-full or add it as a de­pen­dency to our package.json as we have done be­fore, for ex­am­ple package.json:

{
  "name": "vertx3-hello-web",
  "private": true,
  "dependencies": {
    "vertx3-full": "3.0.0-1"
  },
  "scripts": {
    "start": "./node_modules/.bin/vertx run server.js"
  }
}

That’s it for now. Hope­fully this will help you get started work­ing with vert.x!

Next post

My first Vert.x 3 Application

Let's say, you heard someone saying that Vert.x is awesome. Ok great, but you may want to try it by yourself. Well, the next natural question is “where do I start ?”

Read more
Previous post

Checklist for Migrating from Vert.x 2.1.x to Vert.x 3 - Part One

So while upgrading our application, I thought I should note down all the changes that we had to do in the process. Since Vert.x 3 is a major upgrade from the previous version, with so many changes.

Read more
Related posts

Unit and Integration Tests

Let’s refresh our mind about what we developed so far in the introduction to vert.x series. We forgot an important task. We didn’t test the API.

Read more

Checklist for Migrating from Vert.x 2.1.x to Vert.x 3 - Part One

So while upgrading our application, I thought I should note down all the changes that we had to do in the process. Since Vert.x 3 is a major upgrade from the previous version, with so many changes.

Read more

Contract Driven REST Services with Vert.x3

We see a new trend in development where we are shifting from developing applications to APIs. More and more we see services being offered as REST APIs that we are allowed to consume.

Read more