Creating a Nodejs Module

1) Create a package.json using npm init

$ npm init

This 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 fields and exactly what they do.

Use npm install <pkg> --save afterwards to install a package and save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (mymodule) mymodule
version: (0.0.0) 1.0.0
description: hasCode.com Node Module
entry point: (index.js)
test command:
git repository: https://github.com/hascode/hascode-nodejs-module.git
keywords: tutorial test example
author: Micha Kops
license: (BSD-2-Clause)
About to write to /tmp/mymodule/package.json:

{
    "name": "mymodule",
    "version": "1.0.0",
    "description": "hasCode.com Node Module",
    "main": "index.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
},
    "repository": {
    "type": "git",
    "url": "https://github.com/hascode/hascode-nodejs-module.git"
},
    "keywords": [
    "tutorial",
    "test",
    "example"
],
    "author": "Micha Kops",
    "license": "BSD-2-Clause",
    "bugs": {
    "url": "https://github.com/hascode/hascode-nodejs-module/issues"
},
  "homepage": "https://github.com/hascode/hascode-nodejs-module"
}

Is this ok? (yes) Yes

2) Create exports in index.js

exports.greet = function(){
console.log('greetings from hasCode.com :)');
}

3) Create a test js

var greeter = require('./index.js');
greeter.greet();

4) Test the module

$ node test.js
greetings from hasCode.com :)

Firebug XPath Testing Build in

$x("//div/span/em");

nvm, node, angular Installation

nvm Installation

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.7/install.sh | bash

Add to ~/.profile:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

Test if installed by running

$ nvm

node Installation with nvm

$ nvm ls-remote // select version
$ nvm install v9.2.1

Angular installation with node

npm install -g @angular/cli

Create a custom jQuery selector

The following selector matches all elements with a width greater than 200px. Just run the example on a website like jQuery.com in the Firebug console.

$.extend($.expr[':'], {
    widthGT200Pixels: function(elem) {
    return $(elem).width() > 200;
}
});

$('div:widthGT200Pixels').css('border','1px solid red').click(function() {
  alert('The width of this div exceeds 200px');
});

Afterscriptexecute Event

Allows to hook into the execution of every script tag in a page

document.addEventListener('afterscriptexecute', function(e){
    console.log('exec script:', e.target);
}, false);

jQuery function to center an element

jQuery.fn.center = function () {
    return this.css('position','absolute')
        .css('top', ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + 'px')
        .css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px');
}

// centering an element
$('#jq-footerNavigation').center();

Create Ecmascript and Typescript Definition from Protocol Buffers / Protobuf

First install ts-protoc-gen addon.

npm install ts-protoc-gen

Then generate definitions like this:

# Proto Schemas to read
PROTOS_IN=message.proto

# Path to the plugin
PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR="./generated-js-ts"

protoc \
  --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
  --js_out="import_style=commonjs,binary:${OUT_DIR}" \
  --ts_out="${OUT_DIR}" \
  ${PROTOS_IN}