Node Application Patterns

Setting up your project

  • Setting up task runner Grunt

Beginner with Grunt? Where to start? Go here:

Now, let’s set up simple Gruntfile.Create gruntfile.js in your root folder.Here is the sample gruntfile :

So, what this gruntfile does?Take a look at it, first, jshint is use for watch js files for any error in syntax, and the files jshint will affect will be put in the “files” with an array of js files. “watch” is simply you do not want to terminate the terminal any time you start grunt, then “watch ” will help you this time. What “watch” does is it will watch all the changes in your files and auto rerun grunt. Now, let’s add plugins for jshint and watch and register default task. Go to terminal and grunt , you should see

  • Understand modules in Node

Now, let create app.js in your root directory. Then create a folder called utility, inside create utility.js

Now, you will understand what is understand as a models in node.Here is the utility.js

And app.js

and boom, let check node app.js

  • Organisation/Structure of files

Structure of files should be in functionalities. Now, let create these folers inside the root dir: accounting, fulfillment. membership and sales. Let’s create a new package.json and Gruntfile.js inside the sales dir. You will later see what this does. Remember to name the package.json name adding with — sales that makes more sense.

  • Choose your test framework

There are a lot of test frameworks to choose to work with Node. Just pick one and it should be fine. I will choose mocha because I more have experience with mocha:) . Now let install mocha and should dependency using npm and create a sample.test.js

Back in the terminal: mocha. You should pass the test!

  • Control version GIT

Add .gitignore file inside the membership foler. Create a new repo in your github and commit your file there. Full of instructions how to do goes here :

Advanced! How you can add membership folder as a dependency for other folder and reference it the devDependencies in package.json? You can actually do it! You can actually reference the package inside the package.json of that file. For example, membership:newModule/helloMembership(your github repo file name)` in the dependencies of package.json

Let’s build a registration module

Learning Javascript design pattern:

Module Javascript design pattern: CommonJS

Sorry, stop here a bit, will come back later.<to be continued />

Show your support

Clapping shows how much you appreciated Hien Nguyen’s story.