Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Lenny Markus, who has been involved in technology in one way or another over the last 15 years. He is a problem solver at heart and has spent the last two years at @PayPal working on a variety of interesting challenges, trying to find elegant solutions that make life easier for others. He always mentors and try to raise the bar for everyone around him. He can be reached at @lennymarkus.

If you haven’t done so already, you should read my first post, Why should I use Kraken?, where I explain the philosophy behind the Kraken suite. In this post we will explore the creation of a KrakenJS application.

Care and feeding of your pet Kraken

Before we get started, let’s make sure you’ve set up your system correctly. Kraken works best with NodeJS version >= 0.10.12. Be sure that your npm installation is up to date.

In this post, I’m going to create a basic KrakenJS application, explain its structure and point out the features of the Kraken suite.

Installing the generator

The first step in creating a Kraken application is to install the code generator. This generator is written for Yeoman, and similarly to Rails, it can help you quickly create scaffolding for an application.

You install the generator globally (-g flag), using npm:

This command will install the generator and all its dependencies. Once done, we can proceed to create an application.

Generating an application

Go ahead and type the following:

This will invoke Yeoman, and run the Kraken generator. You will be asked a few questions about the new project you want to set up: the Project Name, Description, Author, and optional usage of RequireJS (Say no for now):

The generator will proceed to create a new directory (Project Name), and set up your new application.

Once complete, you can run your application right away:

By default, your application will start on port 8000. You can visit it on and it will greet you.

This is your plain vanilla Kraken app.

What just happened?

The generator created a new application with the following structure:

fig01

There are a few more files, but let’s focus on these in the figure for the time being, going in order of interest.

package.json

This is your package definition file. It contains the name and description of your application, based upon the description you provided, as well as all of the necessary dependencies.

index.js

This is the entry point for your application. When you run “npm start,” this is the file that is invoked. Here is where you can perform any startup configuration that your application might require.

config/

Within the configuration folder, you’ll initially find two files:

  • app.json: It will contain the startup configuration of the app itself, (e.g. port number, host name, and any configuration you want to pass to the underlying express app.
  • middleware.json: It will contain any settings you want to pass along the bundled middleware, and any additional settings that you want to keep separated from the code.

One notable feature of Kraken is that you can have separate configuration files, and they will be loaded by the framework based upon your environment, as controlled by the NODE_ENV environment variable. For example, during app development, you can set NODE_ENV=development, and the framework will look for app-development.json and middleware-development.json with configurations specific to your local machine. When it’s time to deploy to production, setting NODE_ENV=production will load the default files again. Thus, you can swap configurations, without having to touch a single line of code.

controllers/

This is where your routing logic lives. If you take a look now, you will see a single file: index.js. It defines the logic for the / path.

app is simply the underlying Express application, so this should be very familiar to you if you’ve ever worked with Express.

Any .js file that is found in this folder will be automatically scanned for route definitions. The best practice for Kraken applications is to take advantage of this feature by grouping similar paths into separate files. (e.g. /user, /user/new and user/profile should exist within controllers/user.js) This ensures that your application grows in an orderly fashion.

models/

Kraken follows a complete decoupling of application logic, data and presentation. The convention to follow is that any data model objects should be placed in this folder, along with any functionality that they require (Retrieval from a database, class specific functions, etc.) The basic application creates a models/index.js model that simply returns the application name for display.

public/templates/ and locales/

Kraken uses Dustjs for templating. When a view needs to be rendered, you have to specify the name of the template to use. In this case, it’s public/templates/index.js.
If you take a look at this file, you will notice something interesting:

This dust helper is part of the Kraken suite, and its purpose is to enable content bundles to be used with the template. This feature is what allows for Internationalization support with Kraken.

public/template/index.js is going to be tied to one or more similarly named index.properties file. These property files define keys like “greeting”:

And they can be found under a country/language directory structure under locales. For example, in an English/Spanish application, you could find:

and:

That way the content can be decoupled from the template, allowing large teams to collaborate in an easier manner, and making the application more maintainable. Instead of writing multiple templates, and duplicating code, you write one template, with multiple content sources.

Kraken can merge the appropriate content bundle with a template based upon the context of the request.

Gruntfile.js

Kraken has two modes in which it can run: development and production. This is controlled by the environment variable NODE_ENV.

When a Kraken application is being run in development mode (default), it will compile, and localize dust templates on the fly, every time a resource is requested. This saves time for the developer, since a rebuild is not required any time a change is made, but it’s not very performant at run time.

When it’s time to switch to production, the project must be built.

The generated Gruntfile allows you to test and build your project before deployment to production environments. To run the built in tests and linter you can run:

and when it’s time to build do the following:

Next steps

This post is just a primer into what a Kraken project looks like. To learn more about Kraken’s capabilities, take a look at the examples section on KrakenJs.com.

Look below for some great Node.js books (that also contain ExpressJS) from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Node.js Recipes: A Problem-Solution Approach is your one-stop reference for learning how to solve Node.js problems. Node.js is the de facto framework for building JavaScript-powered servers. You will first be introduced to this exciting technology and what it can do, then learn through comprehensive and easy-to-follow recipes that use a problem-solution approach. Node.js Recipes teaches you to fully utilize the Node.js API, and leverage existing modules to build truly exciting projects.
Node.js in Action is an example-driven tutorial that starts at square one and guides you through all the features, techniques, and concepts you’ll need to build production-quality Node applications. You’ll start by learning how to set up your Node development environment, including loading the community-created extensions. Next, you’ll run several simple demonstration programs where you’ll learn the basics of a few common types of Node applications. Then you’ll dive into asynchronous programming, a model Node leverages to lessen application bottlenecks.
Instant Node.js Starter begins with the installation of Node.js and then shows you how to create your first Node.js HTTP server. You will learn how you can use the callback pattern and EventEmitter to control the asynchronous flow and the power of Node.js streams to make your application more scalable and responsive.

Tags: DustJS, Express, Gruntfile.js, Kraken, Kraken.js, KrakenJS, Node.js, NodeJS, package.json, Yeoman,

Comments are closed.