In this post, we'll be exploring the similarities of applications built in Node and JAVA. Rather than going into the specifics of each one, we'll focus on wrapping up the main components to get an app up and running. Ready to get our hands dirty? Let's go!
If you'd like to try the examples presented here by yourself, you can:
More than a build tool, the Node Package Manager (NPM) - which comes as a bonus when we install Node.js - centralizes open-source libraries and is also useful for multiple purposes such as initializing an app from scratch with "npm init", installing dependencies, packaging the app, running it, testing, and more. You can learn more about NPM in more detail here.
The default file for assembling the project specs is the package.json, which can be as simple as the one below:
The "main" defines the JS file that initializes the application, similarly to a Main class in a JAVA project (any class containing the void main(String) method), and there's no standard name for it.
Once we run "npm install" after defining our application dependencies, a folder named "node_modules" is created on the project root folder, which is equivalent to the list of jars that are necessary for a JAVA app to compile.
If we add more dependencies after the first install, it won't create any problems. We can simply specify what we want to install in order to be more precise (e.g. "npm install node-restful"), or simply trigger "npm install" again.
As you might already know, Maven is the most popular build tool for managing JAVA project dependencies. There are a few others available as well, that are less common, such as the excellent Gradle.
Maven's central point is the pom.xml, which is commonly, if not always, saved on the project root folder as the package.json is for Node.
Below is the one used on our sample project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
No hurdles here, with these lines in our main server.js file, we have MongoDB all set:
var mongoose = require('mongoose');
On the JAVA app the configuration is also pretty simple since we are using Spring Data MongoDB, so all we have to do is to define the following on the application.properties:
See how the CRUD managed via RESTful services is structured on the Node application to the left? Not too differently from what we have on the JAVA correspondent to the right.
See the similarities?
Putting it All Together
On our Node app we are linking the pieces starting from the server.js, defining the models, database, routes (which plug the controllers), and finally the application port, whereas on our JAVA app this is in place with annotations (remember that we're getting the advantage of all that Spring Boot can offer along with the most recent JDK). Before long, though, it's likely that we'll have similar support in Node.js. Check it out - attempts are already underway: take a look here.
Final server.js file:
var express = require('express'),
bodyParser = require('body-parser'),
Product = require('./src/model/productModel'),
Category = require('./src/model/categoryModel');
var app = express();
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var productRoutes = require('./src/routes/productRoutes');
var categoryRoutes = require('./src/routes/categoryRoutes');
console.log('Server running at port 3000');
Running the Application
Now it's time to see if our efforts had any results. To test the exposed services, I've used Postman. See below.
Creating a product
Updating a category
Retrieving all products
As we can see above, some of the services exposed by the Node application were successfully executed. To call the API's exposed by the JAVA app and test, all we need to do is change the port to 3001 since the same URI names were chosen and it's already up.
Working with JAVA, many of us have always had contact at one time or another with pure JSP/Servlet, Struts, JSF, or Spring MVC applications. These are still used for some newly created projects, and we'll always face them when working with legacy systems. But the world is changing. With the emphasis on Mobile First development, and the growing concern of building responsive apps that run well, multi-platform, JS-based client side apps are the way to go.
Node.js really worth trying, even taking into consideration that's not recommended yet for any kind of app. Note: I say yet because it's evolving at such a fast pace that this argument will be defeated any day now. It's efficient for both fast development, and enabling lightweight apps to run with minimal effort.
Recommended references besides the official website:
The code mentioned in this article is available for download here, have fun!
What are your thoughts regarding Node? Are you already using it on your projects? Please share with us! =)