In this article we are going to do a quick introduction to Node.js and LoopBack for Ruby on Rails minded developers. What’s LoopBack? It’s an open source framework for easily developing APIs in Node and getting them connected to databases like MongoDB, MySQL, Oracle and SQL Server. We’ll cover installing Node, the StrongLoop CLI, look at an example LoopBack application plus how to start out on your own. All the while drawing parallels between Node and Rails commands.
From Ruby to Node
You’ve likely heard in the past few months that some big companies are starting to deploy Node applications in production and at scale. Walmart is running their entire mobile site on Node, which is currently the biggest Node application, traffic wise. eBay has been running a production Node API service since 2011. PayPal is slowly rebuilding their front-end in Node.
There aren’t that many frameworks that try to deliver Rails experience for Node. LoopBack by StrongLoop aims to be all encompassing solution, similar to Rails. Lets check it out, but first things first…
There are many ways to install Node:
- Download source or binaries and install manually.
- Using Node Version Manager (my preferred way).
- Everything is kept neatly in your user home directory.
- You can easily have multiple versions at the same time.
Package Management In Node
Most of the concepts found in Ruby translate well to Node. Such is the case for package management and NPM is Node’s alternative to GEM. NPM is GEM for Node. After you have setup Node and it’s in your system path, the
npm command becomes available and you can have any of the 70k packages currently published installed with just one command:
$ npm install [PACKAGE NAME]
One of the differentiating factors between NPM and GEM is that NPM installs all packages in the local
node_modules folder by default, which means you don’t have to worry about handling multiple versions of the same package or accidental cross project pollution. The modules folder is a core Node concept that allows programs to load installed packages and I encourage your to read and understand the manual on this subject.
Package Manifest For Node
To help you manage project dependencies Node introduces
package.json as a core concept. On the surface it works similar to
Gemfile in Ruby and contains list modules that your project depends on.
Gemfile for Node.
package.json is a very powerful tool that can be used to run hook script, publish author information, add custom settings and so on.
package.json is just a JSON file, any property that isn’t understood by Node or NPM is ignored and could be used for your own needs.
If you are in a folder with
package.json and want to install all packages it lists, simply type:
$ npm install
This is equivalent to
bundle install in the Ruby world.
Installing StrongLoop API Server
Now that we have covered some basics, lets get started with LoopBack, the open source API framework powering StrongLoop API server.
$ npm install -g strongloop
This will install the
slc command into your global NPM folder, which, depending on the way you setup Node, would either be somewhere in
/usr/local or in your home folder (another reason I prefer NVM). It also installs all the Loopback framework and Devops packages.
slc tool is our gateway for all things StrongLoop. Think of it as
rails command, just for Node. You’ll see why in a moment.
LoopBack Sample Application
$ slc loopback:example
$ [?] Enter a directory name where to create the project: SampleApp
$ cd SampleApp
$ slc run
- Example APIs to explore
- Sample data to play with
- Multiple data adapters (MongoDB, In-Memory, MySQL and OracleDB)
- API explorer with auto-generated API endpoint methods for CRUD
Open up http://localhost:3000/explorer in a browser and
Check out StrongLoop documentation for some additional details about the sample app.
$ slc loopback
[?] Enter a directory name where to create the project: my-project
[?] What's the name of your application? my-app
$ cd my-project
$ slc run
This will generate skeleton LoopBack application (hence
slc loopback). This is equivalent to
rails new myapp.
Lets look at what we have:
$ find . -maxdepth 2
datasources.json stores the backend datasource settings while the
config.json has the application host and port settings.
Models will be stored as .json files under
model-config.json retains the mapping of models to data-sources.
node_modules contains minimum dependencies you need to get your starter application to run.
Adding a Model
$ slc loopback:model
[?] Enter the model name: Person
[?] Enter the data-source to attach Person to: db (memory) // Hit enter to accept default
[?] Expose Person via the REST API ? Yes
[?] Custom plural form (used to build REST URL): People //Hit enter to accept default
Lets add some Person properties now.
Enter an empty property name when done.
[?] Property name: Name // Name the first property in the model
[?] Property type: string // Hit Enter to accept default type, string
[?] Required? No // Hit Enter to accept default (not required)
/common/modelsmodels folder called
It’s worth noting that json files for models is just a default way to store models. In the
At this point you might be thinking that
slc loopback command is similar to
rails generate and it is, just not as elaborate at the moment.
Previously we used
slc run to start the sample application. However, your preferred way to start your application could simply be:
$ node app.js
That’s really all to it, it’s that simple to get started. Check out documentation to get a details on how to expose the newly created model via the REST API and full details on working with models and datasources.
- Check out two additional example apps written with Loopback : Access Control and FoodMe
- Don’t forget to check out the LoopBack.io project home page, docs and GitHub page.
- Need training and certification for Node? Learn more about both the private and open options StrongLoop offers.