Introducing the Technologies
It should come as no surprise that Node.js has quickly been recognized by both small and large enterprises as a technology leader to provide robust, scalable, and feature rich backend services for web and mobile applications. Having been adopted by large companies to provide web service API’s, Node.js is here to stay as it provides a huge increase in the amount of traffic that one can serve given a smaller overall footprint than traditional web services.
The problem that StrongNode addresses is the lack of commercial and world class support for a Node.js distribution. When you are deploying Node.js applications and services in a production environment, StrongLoop can provide the support, resources, training, and performance monitoring that you need to ensure that your applications are robust and highly available.
One of the benefits of using StrongNode is the ability to install and use trusted NPM modules that has been fully vetted for security and performance issues by the experts who know the internals of Node.js. This allows you to install, use, and deploy modules that you can trust to run your enterprise quality applications.
MongoDB was created with scalability in mind with the concept of sharding and replica sets. The will ensure the your highly available StrongLoop Node applications will be performant not only at the application code level but also on the datastore backend.
Appcelerator also provides an IDE called Titanium Studio to help developers of the platform be more productive. For those developers who prefer to use the command line, tools are also provided to compile your mobile applications without using the Eclipse based IDE.
Downloading and Installing StrongNode
To get started with our sample application, the first thing we need to do is download and install the latest release of the StrongNode distribution. Point your browser the download page and begin the download process by entering in your email address and selecting the operating system that you will be using for development.
Note: For this blog series, I will be using Mac OS X but the instructions and command line tools should be portable across the various supported operating systems.
Once your download has completed, click on the .pkg file to begin the installation process.
Follow the onscreen instructions to install StrongNode (node, npm, and slc).
Pretty simple, huh? You now have an enterprise ready and trusted distribution of Node.js ready for you to begin development with.
Creating the StrongNode application
One of the many benefits of using StrongNode is the ability to take advantage of the convenient command line utilities to quickly create applications that are stubbed out for building backend REST services. Given that we are going to be using MongoDB as our datastore, we can take advantage of the Mongoose library that the StrongNode distribution provides. To create our web application, enter in the following command:
$ slc create web mobilebackend -m -r
What just happened?
Let’s take a closer look at the command above and fully explain each option:
slc : This is the binary that StrongLoop provides to interact with the distribution. All actions that you want to perform using StrongNode will use this binary. slnode will accept many arguments and for the full list, simply enter in the following command: $ slc -h
slc create : The first argument that we pass in is create. This will notify the distribution that we want to perform the create action followed by the type of resource that we want to create. At the time of this writing, the available types to create are cli, module, package, or web.
slc create web : We specify that we want to create an express application for creating our REST based services.
slc create web mobilebackend : After specifying that we want to create an express based application, we give the application the name of mobilebackend. You can provide any name that you want your application to be.
slc create web mobilebackend -m : The -m option will install and configure Mongoose support so that we can interact with the MongoDB database that we will create later in this blog series.
slc create web mobilebackend -m -r : We finally add the -r option which will add REST apis for any Mongoose models that we create.
Testing your application
After the create application command has finished, we have a fully functioning application created for us to start development with. Before we move any further, we should verify that everything went smoothly by running the application using the slc command. To start the application, change to the application directory and start the server using the following command from a terminal prompt:
$ cd mobilebackend
$ slc app.js
Unless you already have MongoDB installed locally, you will see an error message indicating that the mobilebackend application could not connect to [localhost:27017]. This is expected. Point your browser to the following URL to test the application:
If the server started correctly, you should see the following page:
Congratulations, you have just created and deployed an application that supports MongoDB, Mongoose, and REST APIs using the StrongLoop distribution.
Understanding the Default Application
To fully understand a default StrongNode created application, let’s examine all the pieces that were created for our application and explain what each one is used for. If you change to your mobilebackend directory, you should see the following files and directories:
The app.js file, located in your application root directory, is the bootstrap file that sets up and configures your application. This is also where some default dependencies are required such as express, http, and path. This is a pretty standard app.js file that creates a HTTP server to handle requests and sets up default routes.
The db directory is the location for — you guessed it — configuration files to specify the location of the MongoDB datastore that you want to connect to. Go ahead and leave the defaults for now as we will be setting up MongoDB later in this blog series. When we are ready to connect to our database, we will be modifying the config.js file in this directory.
The models directory is where your Mongoose models should be created. By default, a User model was created as an example to explain the syntax while creating new ones.
The node_modules directory is where any modules that you have added as dependencies for your application will be stored. Module dependencies are specified in the package.json file. For the most part, you can ignore this directory as we will not need to use it with the sample application that we are going to write for this blog series.
Most modern programming languages offer a dependencies resolution mechanism to allow developers to depend on and include third party libraries. For Node.js, that mechanism is the package.json file. If you take a look at the default file that StrongLoop Node created for us, you will notice the distribution has included dependencies to allow us to use the Express Framework, interact with MongoDB, and other useful APIs.
At the time of this writing, there are over 30,000 available NPM Modules that you can include in your application to quickly take advantage of existing functions to speed up your application development.
This is where the default stylesheet for our application is located.
The resource.js file in the routes directory is where the basic create, read, update and delete (CRUD) methods are defined for any Mongoose Model Objects that we create. By default, the following functions are available for any model that you create:
Having these basic functions available to any model object that you create is a significant advantage in that you can reduce the amount of code you have to write and maintain for common database operations. If you need more complex queries or if you need to specify the sort order for a result set, you will need to create your own functions to handle those queries.
The views directory is where the views for your application will be stored. By default, our views will use the EJS templating system. For more information about the powerful templating system, head over to the project website.
In this blog post, we discovered how to install the StrongNode distribution. We also learned how to create a web application that is pre-wired for connection to MongodB, using Mongoose, and also has the ability to quickly create REST based web services.
In the next post, we will create our own REST web service and connect the application to a MongoDB Database.
- Ready to develop APIs in Node.js and get them connected to your data? Check out the Node.js LoopBack framework. We’ve made it easy to get started either locally or on your favorite cloud, with a simple npm install.