Getting started with Express.js for Java Developers

Learning a new technology is always challenging. Recently, I started to play around with the serverside programming side of JavaScript with Node.js and Express.js. I created a very simple app. Then I thought to myself why not share my experience and create some posts for those who may be interested. In this article, I cover how to getting started with Node.js and Express.js for Java Developers. And in the following posts, we cover more stuff as we progress. It is intentionally broken into multiple posts to not overwhelm you.

A little bit of background

To know who is the target audience for this tutorial, I start by giving a little bit of background.

I know some JavaScript but by no means, I’m an expert on it. My experience was limited to frontend JavaScript and had no background with Node.js. Out of boredom, I decided to create a simple app with Node.js and Express.js that has APIs for CRUD operations.

I must say as a Java developer, I’m amazed at how things are much simpler and easier in Node.JS. No need to mention that it’s lightweight and the compilation is almost instant.

Who’s the target audience?

This guide is mainly written for those who know how to code. It is particularly written for Java developers. If you are good in a language and a web framework understanding it won’t be a problem.

However, if you don’t have much knowledge of coding or just get started, I’m afraid this article is not for you.

What we implement?

We create a very simple app that has some rest APIs to do CRUD operations. Nothing is fancy. To be able to create rest APIs we rely on Express.js.

Express.js is a Node.js library that is specifically developed for rest API creation. It’s very lightweight and easy to use. It’s similar to Flask in Python world. Or Spring Boot, but of course much much more limited.

In this article, we only cover how to set up the environment and create the base project.

Installing Node.js

Before we do anything we have to install Node.js. But what’s Node.js? Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a browser. It lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser (Wikipedia). Node.js uses V8 execution engine to compile JS code to machine code a runtime.

To install Node.js, go to https://nodejs.org/en/download/ page and download the appropriate package according to your OS. The process is fairly straight forward, so I won’t demonstrate it in details here.

Setting up the project

In this section, I go through how to set the project up. It consists of small steps that I’ve broken them down for better understanding.

Creating the project with npm

After you have installed Node.js, we can leverage on the goodies that came with it. One of them is npm. It stands for Node Package Manager. It is similar to Maven or Gradle in Java world.

So to set the project up we relay on npm. This means we can add dependencies and manage the versioning. The versioning in npm is not as hassle-free as Maven.

The user needs to remember to commit package-lock.json file that tracks down which library and which version of it is used in the project. Without that file soon or late the project breaks because again by the contrast of Java world, JS libraries most of the time are not backward compatible.

Now that you know about the npm let’s create the project. To create the project we need to type,

$ npm init

Once you ran the above command, provide the package name, version, description, entry point, etc. Or just press enter if defaults suit you.

Adding Express.js

Then we need to add Express.js package,

$ npm install express --save

--save argument adds the package to package-lock.json and package.json files. Without that npm only installs the package.

Configuring Babel

And then we add Babel,

$ npm install babel-cli --save
$ npm install babel-preset-es2015 --save

Babel is a transcompiler that compiles ES6 syntax to ES5. But why? To make it compatible with old platforms. But why not code in ES5? Imagine that you can code in Java 8 but you are forced to code in Java 7. How do you feel about?

Apart from the joking, ES6 is similar to Java 8. Bunch of functional goodies and cool features introduced so it makes sense to code in ES6 and transcompile it to be compatible with ES5. And for that, we rely on Babel.

Once you add the babel you need to configure it via .babelrc. This file should be in the root project directory. And since we would like to convert ES6 to ES5 we can do "presets": ["es2015"] with the below command,

$ echo '{ "presets": ["es2015"] }' >> .babelrc

Body parser package

The next step is to add the body-parser library. This is for parsing the POST requests body. Surprised huh? To add that just run,

$ npm install --save body-parser

Adding nodemon

Now more or less the project is ready and we can jump-start the development. But to make the development process more pleasant, let’s add one last library. This library is nodemon. It auto compiles the code when it changes. In other words, it does live reload, just change the file, save and then you will see the result immediately. It’s similar to Spring Boot Dev Tools and Tomcat Hot Swap to an extent.

So to have it, all you have to do is run this command,

$ npm install nodemon --save-dev

Note the --save-dev argument. It indicates that the module is for the development of the application and not require while running it in production environment.

And add the following lines to package.json file,

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "nodemon app.js --exec babel-node --"
}

Congratulations you build your first Express.js app.

That’s all for this tutorial, I hope you enjoy it. Stay tuned for the next part.

Inline/featured images credits