Download events in aria2

In my previous post, I have demonstrated how to use aria2 as the default download manager on Unix/Linux and how to integrate aria2 into Conkeror. In this post, I’m going to show a simple tip that can cause aria2 to display notification when it finishes downloading. By default, aria2 provides some input arguments which are the event handlers for download hooks. Those events include --on-bt-download-complete, --on-download-complete, --on-download-error, --on-download-pause, --on-download-start, --on-download-stop. The solution is to pass a shell script to the event and use that bash script to activate notification program. For example

$ aria2c --on-download-complete="/path/to/notification/script.sh" http://example.com/file.iso

You can also pass the event handler command to the aria2 command for starting it as daemon or on RPC protocol. If you are using the command from my previous post, the command will look like this

$ touch /path/to/download/folder/session.txt && aria2c --enable-rpc --rpc-listen-all --save-session=/path/to/download/folder/session.txt --input-file=/path/to/download/folder/session.txt -x16 -s16 -k1M --dir=/path/to/download/folder --daemon --on-download-complete=/path/to/notification/script.sh

When aria2 finishes downloading, it will run the –on-download-complete script and pass 3 arguments, the third one is the path to the downloaded file. The content of the notification script.sh file should look similar to this

#!/bin/sh
notification-command "Download complete $3"
Read more

1. PassportJS

PassportJS is an authentication module for NodeJS which uses the standard Connect middleware structure. As a result, it is convenient to integrate with any applications that operate on the middleware structure like Express. If you haven’t known about Connect middleware yet, take a look at this post Nodejs with Express - More advanced stuff. Passport supports many authentication methods (strategies) like Local (using username and password), OAuth, OpenID or through Facebook, Google, Twitter,…

To use PassportJS, install it as a dependency for your project

$ npm install --save passport

Next, add this to your main app.js file. Put it after initializing session

var passport = require('passport');

var app = express();

app.configure(function(){

  /* other config goes here */

  // put passport config after this line
  app.use(express.session());

  // passport initialization
  app.use(passport.initialize());
  app.use(passport.session());
});
Read more

One of the most important feature introduced in Javascript ES6 is the Generator function, which provides the ability to suspend the execution of a function. However, not all browsers and platforms support generator right now. Earlier versions of Firefox either had ES6 generators turned off, or supported only old style generators. On Chrome 28, you have to turn on an experimental flag named Enable Experimental JavaScript. Node.js haven’t got generator support until version 0.11.2 and you have to run it with --harmony flag.

You have another option that is to compile the javascript files using Regenerator, an ES6 to ES5 Javascript compiler (Google also has their own compiler called Traceur). Regenerator is available on npm

$ npm install -g regenerator

The command to transform js file is simple. You need to include the runtime file in your result html file.

$ regenerator es6.js > es5.js

If you want Regenerator to include the runtime automatically for you in all files that it generates, use --include-runtime flag

$ regenerator --include-runtime es6.js > es5.js
Read more

Internationalization (i18n) is one essential part of any application. This can be achieved easily in Nodejs with the the module i18n. There is another module called i18n-2, which is based on i18n and designed specifically to work out-of-the-box with Express.js. In this post, I will focus mostly on i18n-2.

First, you need to install i18n-2 using npm

$ npm install --save i18n-2

After that, add these config line into your app.js file. Remember to add it after you have loaded the cookieParser.

app.use(express.cookieParser('your secret here')); // put the config after this line

i18n.expressBind(app, {
  // setup some locales - other locales default to vi silently
  locales: ['vi', 'en'],
  // set the default locale
  defaultLocale: 'vi',
  // set the cookie name
  cookieName: 'locale'
});

// set up the middleware
app.use(function(req, res, next) {
  req.i18n.setLocaleFromQuery();
  req.i18n.setLocaleFromCookie();
  next();
});
Read more

1. Browserify in the command line

Browserify helps you modularize your client side Javascript code using Nodejs require style. It also supports transforming Nodejs modules on npm to browser compatible ones. You can install browserify using npm

$ npm install -g browserify

Writing code with browserify is pretty easy and familiar with Nodejs developers. Just write your code in Nodejs require style and then let browserify handle the rest for you. For example you have 2 files, foo.js and main.js with the content like this

  • foo.js
module.exports = function() {
    // do something and return value here
    return 1;
};
  • main.js
// include foo.js here
var foo = require('./foo.js');

// you can also include nodejs/npm modules. this example includes d3-browserify installed
// using npm (npm install d3-browserify)
var d3 = require('d3-browserify');

// call the foo function
foo();  // returns 1

// call the d3
var tree = d3.layout.tree(); // create a tree layout in d3
Read more

Full series: Building website with Nodejs - A post from my experience
Previous post: Nodejs - Express with ejs/stylus basics

Middleware

Simply, middleware are just functions for handling requests from client. Each request can have be associated with a stack of middleware. That means when you have several of handler functions associated with one request, they will be executed sequentially. Back to the example from my previous post

app.get('/', middleware1);

function middleware1(req, res){
  res.render('index', { title: 'Express', check: true });
};

In this example, there is only one middleware (handler) function for the GET request to /. Now we will add one more middleware, or maybe more if you want

app.get('/', middleware1, middleware2);

function middleware1(req, res, next){
  // do something here
  // ...

  // activate the next middleware, otherwise, the next middleware will never be
  // called and the client will wait until timeout
  next();
};

function middleware2(req, res){
  res.render('index', { title: 'Express', check: true });
};
Read more

Full series: Building website with Nodejs - A post from my experience
Previous post: Install and Create basic structure for Nodejs website

package.json file

package.json file is used for storing information about your application as well as its dependencies. Usually, when managing your Nodejs application with git, you should ignore the node_modules folder (which contains all the dependencies). Instead, you can specify the library that your app use and then let npm install it automatically for you with npm install. Here is the default package.json file generated by express

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.8",
    "ejs": "*",
    "stylus": "*"
  }
}

You don’t even need to specify the dependencies manually. When you install any new package with npm, just add --save flag so that npm will update the package.json file automatically for you. For example

Read more

Full series: Building website with Nodejs - A post from my experience

Install Nodejs, npm using nvm

There are several ways of installing Nodejs. You can install it using your OS packages manager or compiling it from source. However, the recommended way is to install it using nvm. nvm can help you install, maintain and run multiple versions of Nodejs (without sudo need).

To install nvm, simply do

$ git clone https://github.com/creationix/nvm.git ~/.nvm

Add this to your shell’s rc file

$ source ~/.nvm/nvm.sh

Next, install one version of nodejs that you want using nvm and set it as the default

$ nvm install 0.10
$ nvm alias default 0.10

Installing nodejs with nvm will automatically install npm (packages manager for Node). For more information about nvm, visit it’s homepage at https://github.com/creationix/nvm.

To test whether nodejs works correctly, you can use the example from nodejs page. Create a file named example.js

Read more

1. Introduction

Currently, I’m building a website written entirely in Javascript using NodeJS along with PostgreSQL database server. This series of tutorials is a summary of my experience with NodeJS, PostgreSQL as well as how I solve the problems I encountered. In this first post, I will give an outline of which frameworks, technologies and the list of all following tutorials in this series. The reason why I chose those technologies will be presented in the corresponding post.

2. Frameworks / Technologies

Here is the list of some main frameworks, libraries and technologies that I used for developing the website.

2.1 Database

2.2 Backend

Read more

Setting up ssl connection can be different for different kinds of server. To make the configuration process easier, we can use nginx as a https proxy server. In this design, the client connects to the nginx server using https with encrypted data. After that, nginx decrypts the data and forwards it to the real web server (also running locally in the same server with nginx). This post demonstrates the steps for configuring nginx as an https proxy server.

Installation

First, you need to install nginx with ssl support. On Mac OS, by default, Macports does not install ssl for nginx, you need to use this command

$ port install nginx +ssl

Next, you need to find out the configuration file for nginx. Usually, it is located under /etc/nginx (or /opt/local/etc/nginx for macports version). There are already some sample configuration files with the .default extension there for you. You can use those sample config files by removing the .default extension.

Usually, you don’t put all the settings in nginx.conf file. Instead, you can create another directory for storing your own ones and include them in the main config file. For example, you put all your config files inside site-enabled folder, add this inside the http section of the nginx.conf file.

http {

    # ...
    # other config
    # ...

    include sites-enabled/*;
}
Read more