Tag: Node.js

0

Using Gulp.js to check your code quality

In this blog post I want to show you how you can use Gulp.js to automate some tasks to check the quality of your code. Before we deep dive into the subject and the coding examples I first want togive you a short introduction on what Gulp.js actually is. So if you already know what Gulp.js is about you can move on to the next chapter. Easy to useBy preferring code over configuration, gulp keeps things simple and makes complex tasks manageable.EfficientUsing the power of node streams, gulp gives you fast builds that don’t write intermediary files to disk.Previous statements are quoted from the Gulp.js homepage. Gulp.js is just like Grunt.js a task runner build on Node.js aka io.js. Where you define your tasks in Grunt.js in a configuration based style, you will be defining your tasks in Gulp.js more in a code based style. For both task runner there is a wide variety of plugins available. So what is the real power of Gulp.js? In my opinion that is the utilization of the Node.js streams, which makes Gulp.js a very fast and memory efficient task runner. The difference between Gulp.js and Grunt.js can especially be noticed when working on larger projects, with huge amounts of files. In many cases developers are limiting the usage of both task runners to only their javascript projects. As I showed you last time Grunt.js can also be used to automate some of your .Net/c# tasks I want to show you today you can also use it on php projects. So here is my call for action. Stop limiting yourself and try to apply it on any project you’re working on, no matter the language!

0

Using Mocha Chai Sinon to test Node.js

In this article I’m going to show you how to write tests for your NodeJS application using Mocha, Chai and Sinon. Mocha is a feature-rich JavaScript test framework running on node.js and the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. One of the cool things is you can choose your own assertion style when writing Mocha tests. In this article I will use Chai to do my assertions. Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any JavaScript testing framework. Chai supports 3 assertion styles. Should, expect and assert. This makes Mocha and Chai the ultimate combination to make your testing suite completely fit your own project needs and desires. As a mocking framework I choose Sinon since it integrates neatly with Mocha and Chai and dozens of other test frameworks. Standalone test spies, stubs and mocks for JavaScript. No dependencies, works with any unit testing framework. In order to start with writing your tests we first need to install Mocha, Chai and Sinon. Since I use Mocha for multiple projects I choose to install Mocha globally. 12npm install -g mochanpm install --save-dev mocha chai sinon To be sure the consumers of my node package also have mocha installed I also add it to the dev dependencies. Since I installed Mocha globally it won’t be installed in my package folder again. Now we can actually start writing our tests.

0

Automate your development tasks using Grunt

Grunt is an extremely useful Node.js package to automate lots of development and continuous integration tasks. The Grunt eco-system has lots of packages available on npm. This enables us to quickly setup our development/continuous integration environment. Grunt tasks mostly have two required properties. An files array, which is used to configure on what files the tasks is executed, and an options property which configures some task specific settings. The files array supports the globbing and minimatch pattern to match files based on the provided expression. So what tasks could you use for your projects, or for which project can you use Grunt? How do I configure Grunt tasks? How do I execute them? All these questions I try to answer for you in this article.

0

npm tips and tricks

In my previous post I showed you how easily you can create a simple webserver using Node.js. In this post I want to show you how to make more advanced usage of node package manager. npm initUsing node package manager you can get an even quicker start of your project by using the npm init command. So let’s get started by opening a command prompt (on windows open the Node.js command prompt). Then create a new folder and navigate into this newly created folder. In the folder execute following command and answer the questions or press enter for the defaults. 1234567891011121314151617181920212223242526272829303132333435363738394041424344Your environment has been set up for using Node.js 0.10.17 (x64) and npm.Press any key to continue . . .C:\Users\Marco> mkdir NodeJsPackageExampleC:\Users\Marco> cd NodeJsPackageExampleC:\Users\Marco\NodeJsPackageExample> npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sane defaults.See `npm help json` for definitive documentation on these fieldsand exactly what they do.Use `npm install --save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^C at any time to quit.name: (NodeJsPackageExample) node-js-package-exampleversion: (0.0.0) 0.0.1description: Total package awesomenessentry point: (index.js)test command:git repository:keywords: package, awesomenessauthor: Marco Franssenlicense: (BSD-2-Clause) MITAbout to write to C:\Users\Marco\NodeJsPackageExample\package.json:{ "name": "node-js-package-example", "version": "0.0.1", "description": "Total package awesomeness", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "package", "awesomeness" ], "author": "Marco Franssen", "license": "MIT"}Is this ok? (yes) yes After answering the questions the result should be something like above. As you can see I have changed the default name. By default node package manager uses the folder name. Play around yourself to get the desired result. You can also change the package.json after it is created. Why do we need this package.json file?

0

Starting with a Node.js webserver

Before starting to explain how you start your first Node.js project for building a simple web server I will first explain you what Node.js is. To do so I just include a quote of the Node.js themself, because I don’t like to reinvent the wheel. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. The next thing you should know is about the Node.js Package Manager (NPM). Using NPM you can benefit from the thousands of open source packages so you won’t have to build everything yourself. I suppose you already have installed Node.js. Let’s start with a simple Hello World in Node.js by creating a file called hello-world.js and execute our script to show the result. hello-world.js1console.log('Hello World'); We can execute the script using following command. (On Windows open the Node.js command prompt) Navigate to your folder containing the just created hello-world.js file and execute the script using node. 123456Your environment has been set up for using Node.js 4.1.1 (x64) and npm.Press any key to continue . . .C:\Users\Marco> cd MyHelloWorldCodeFolderC:\Users\Marco\MyHelloWorldCodeFolder> node hello-world.jsHello World