Tag Archives: node.js


I was studying (rev-eng) how does YouTube works when I’ve found this post about the get_video_info call.

After some digging and some trying I’ve made a node.js module to make the request and parse the response, without the need for the official API.

Here’s the link:

Leave a comment

Posted by on 2016/07/29 in dev


Tags: , , , ,

How-to handle app parameters in Node.js

Let say you have to handle one or more custom parameters in your Node.js app. There are many ways to do that, here are my fab 4.
In these examples I’ll read a debug (on/off) and a port (number) parameters.

keep an eye on these two modules:


Passing them to the interpreter

node app.js --debug --port 8080
var dbg = (process.argv.indexOf('--debug') >= 0);
var port = 0;
if (process.argv.indexOf('--port') >= 0 && (process.argv.indexOf('--port')  +1 < process.argv.length))
  port = process.argv.indexOf('--port') + 1;


Adding them to the package.json

 "config": {
   "port": 8080,
   "debug": "true"
var dbg = (process.env.npm_package_config_debug == 'true');
var port = process.env.npm_package_config_port || 0;

If you’re using PM2, you also need to create an ecosystem.json file ( to correctly handle the package.json stuff.

Using a local json file (params.json)

  "debug": "true",
  "port": 8080
var pars = require('./params.json');
console.log(pars.port, pars.debug);


Using an environment variable (not my preferred …)

export debug=true
export port=8080
var dbg = (process.env.debug == 'true');
var port = process.env.port || 0;
Leave a comment

Posted by on 2016/07/07 in dev


Tags: , ,

I promise you a working jsdom

Working with jsdom is incredible, but most of the times you feel drowning in the callback hell
Here’s a template (soon on my github) that makes everything oven-ready 😉

UPDATE: added V2 that handles the NEW jsdom (see below)

And, as always, under 100 lines of code! =)

Leave a comment

Posted by on 2015/10/15 in dev


Tags: , , ,

I’m starting to wear Mustaches

It sounds easy, but the recipe includes Mustache/Handlebars Jade and un-named arrays of items 🙂

Who renders?

I love the Jade syntax and it’s natural to me to use it in the Express.js workflow. You write (clean!) templates, the middleware does the rest.
Everything looks good but I was missing a template engine for javascript objects. I used Angular.js but it is a fence too tight to me (I happily embrace the single responsibility principle).

So I tried Handlebars and (with a couple of tweaks) it works like a charm! Just remember the workflow:

  • Express.js renders (server-side) the Jade template into plain html
  • the browser renders (client-side) the Handlebars template previously generated


  1. assign to the container tag a custom attribute (I use something like “< ul handlebars=users …“)
  2. write the handlebar template inside the container
  3. bind the javascript context to the data attribute of the container ($(“[handlebars=users]”).data([…])). Please note I’m using an array
  4. render the whole thing

The render engine

This is easy: take the data, take the html, mix together, done.

 $("[handlebars=users]").data("hbar", [
  { _id:1, name:"test 1" }
  , { _id:2, name:"test 2" }
  , { _id:3, name:"test 42" }


$(document).ready(function() {
  $("[handlebars]").each(function(i,o) {
    var $o = $(o);
    var data = $;
    var template = Handlebars.compile($o.html());

The previous code doesn’t work very well with json over web-services and late bindings, so I built a function to do this:

function handlebar_bind($elem, url) {
  if ($elem && $elem.length>=0) {
    if (typeof url == "string") {
        url: url,
        //jsonp: "callback",
        //dataType: "jsonp",
        dataType: "json",
        success: function(data) {
          var template = Handlebars.compile($elem.html());
        error: function(XHR, textStatus, errorThrown) {
    else { // url is an object or array
      var template = Handlebars.compile($elem.html());


Do you remember we bound an array to the html tag? Now you can use the built-in each helper, remembering that this and . refers to the context’s item.
I’ll do the binding via the following script:

{{#each .}}

I also found this syntax (more readable):

{{#each . as |user|}}

…and Jade?

The safest way to add a handlebar template to the Jade syntax is to use comments:

  // {{#each .}}
  span {{this}}
  // {{/each}} 

It will be rendered as an html comment and it will not hurts your html. I do prefer comments because they don’t break the DOM (i.e inside a table BUT outside a td), but many use plain text instead:

| {{#each .}}
Leave a comment

Posted by on 2015/06/16 in dev


Tags: , , , ,

Dynamic sql from Edge.js


I’m exploring the incredibles potential of the Edge.js library, that permits you to execute .NET (Python, Powershell, ..) code within Node.js.
In particular I’m focusing on the edge-sql module.

The sql module allows you to execute a “select,insert,update,delete,exec” statement against a SQL Server database. All you have to do is to set an environment variable (EDGE_SQL_CONNECTION_STRING) with the connection string, but you can also use a connectionString parameter as we’ll see.

The .func(..) function of edge takes two parameters: language (string) and parameters (object) and returns a function that takes your parameters in input, as an object literal.

For further reading let’s go here.

Snippet time!

This is dynamic sql passed to our database! Whoa!

var edge = require('edge');

var myselect = edge.func('sql', {
  connectionString: "Data Source=.\\DEV;Database=mydb;UID=sa;PWD=mypassword"
  , source: function () {/*
    exec sp_executesql

myselect({stm:"SELECT * FROM sys.databases"}, function (error, result) {
  if (error) throw error;
Leave a comment

Posted by on 2015/06/04 in dev


Tags: , , ,

Express.js and jsonp

Express.js has a very good jsonp handler, the key is to use it in the right way =)

The first step is to ensure the callback name. The default value is “callback” so on a res.jsonp(..) the server will look for req.query[“callback”] to build the response.
This value can be overridden with an app.set(‘jsonp callback name’,..).


app.get('/', function(req, res) {
  var jsonp_callback = app.get('jsonp callback name');
  var jsonp = (req.query[jsonp_callback]);
  var result = [{},{},.....];
  if (jsonp) res.jsonp(result);
  else res.json(result);


When asking for jsonp stuff, jQuery will handle the whole request/response stuff.
We just need to say the dataType and the callback key.

Here’s an example of the client side call:

  url: "http://localhost:3000/",
  jsonp: "callback",
  dataType: "jsonp",
  success: function( response ) {
    console.log( response );
  error: function(XHR, textStatus, errorThrown) {


Angular will expect the callback will be named “JSON_CALLBACK” so we have to use it this way:

  .success(function(data, status, headers, config) {
    console.log( data );
  .error(function(data, status, headers, config) {
Leave a comment

Posted by on 2015/02/04 in dev


Tags: , , , ,