Vert.x 3 says “hello” to NPM users
In programming literature it has become the standard to create a hello world program as the first example. In this
article, I’ll be demonstrating how NPM
users can quickly get started with vert.x
. You will see that it is not
that different and in fact it can be done using the tools you’re used to. Note that although we are using NPM
we are
not relying on node.js
, all javascript
code runs on the JVM
.
Hello World Examples
Here are four simple hello world examples. The comments in the code explain how the code works and the text around it explain what it does and how to test it.
Hello Console
This example is about as plain as it can get. It prints the words “Hello World
” to the terminal. If you’re a
javascript developer you should be already used to npm
and know that you always start a project with the file
package.json
:
Note that we have a dependency wich is obvious vert.x
now note that there are 3 flavours of this dependency:
According to your needs you can pick a different flavour, since for a simple hello world we only need the minimal that is the one we add to the dependency property.
Now we need to do a simple hello app, we will call this file “server.js
”:
You can run this by executing:
The first command retrieve the vert.x stack while the seconds starts your program.
Hello HTTP
I’d guess that while it’s not the only use case for vert.x
, most people are using it as a web application platform. So
the next example will be a simple HTTP server that responds to every request with the plain text message “Hello World
”
server.js
:
Now you can reuse the same package.json
we’ve just defined in the previous section and start the server with
npm start
. Once the server starts you can open a browser to http://localhost:8080
and enjoy the message.
Hello TCP
Vert.x
also makes an excellent TCP server, and here is an example that responds to all TCP connections with the
message “Hello World” and then closes the connection server.js
:
Again reuse the previous package.json
and test it by doing telnet localhost 7000
.
Hello Web
Often you won’t be using vert.x
built-in libraries because they are designed to be very low level. This makes vert.x
quick, nimble, and easy to maintain, but if you are planning to build a complex application you want some productivity
and rely on a simple web framework. For this specific case there is vert.x web
,
a simple, yet productive framework, to build fast web application with routing, template
rendering, lots of middleware etc…usually not enough to get started on a real world application. This example shows an
HTTP server that responds with “Hello World” to all requests to ”/” and responds with a 404 error to everything else
server.js
:
In order to test this, you will need to install the vertx3-full
stack. There are two ways to do this. You can either
install it globally npm install -g vertx3-full
or add it as a dependency to our package.json
as we have done before,
for example package.json
:
That’s it for now. Hopefully this will help you get started working with vert.x
!