NodeJS Version Upgrade 5.1.0 and Error Handling

NodeJS Version Upgrade 5.1.0 and Error Handling

1 Version Upgrade
4.2.2 on Local Mac OS
https://nodejs.org/dist/v4.2.2/

I like to build all these things on my own server. Download the source version.
> wget https://nodejs.org/dist/v4.2.2/node-v4.2.2.tar.gz

> ./configure --prefix=/Users/carl/tool/node-v4.2.2

Make and make install there. Add it to the class path.

5.1.0 on Local Mac OS
https://nodejs.org/dist/v5.1.0/

> wget https://nodejs.org/dist/v5.1.0/node-v5.1.0.tar.gz

> ./configure --prefix=/Users/carl/tool/node-v5.1.0

Do the same thing as 4.2.2.
> node --version && npm --version
v5.1.0
3.3.12

5.1.0 on CentOS/Linux
Prepare the build env
> sudo yum groupinstall "Development Tools"

Same thing as on MAC

Add things to the path as follow:
>vi ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:$HOME/bin

JAVA_HOME=/opt/jdk1.8.0_60
export JAVA_HOME

JRE_HOME=/opt/jdk1.8.0_60/jre
export JRE_HOME

PATH=$PATH:/opt/jdk1.8.0_60/bin:/opt/jdk1.8.0_60/jre/bin
PATH=$PATH:/opt/sbt/bin
PATH=$PATH:/opt/node/bin

export PATH

Done
> node --version && npm --version
v5.1.0
3.3.12

5.1.0 on Ubuntu System
Same as on MAC OS

5.1.0 on RaspberryPi
Currently version is as follow:
> node --version && npm --version
v0.12.7
2.11.3

Check OS version
> uname -a
Linux raspberrypi1 4.1.7+ #817 PREEMPT Sat Sep 19 15:25:36 BST 2015 armv6l GNU/Linux

> uname -a
Linux raspberrypi2 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux

Oh, my different raspberryPis are different.
On ARMV61
> wget https://nodejs.org/dist/v5.1.0/node-v5.1.0-linux-armv6l.tar.gz

Put it in the working directory
> mv node-v5.1.0-linux-armv6l ~/tool/node-v5.1.0

> sudo ln -s /home/carl/tool/node-v5.1.0 /opt/node-v5.1.0

> sudo ln -s /opt/node-v5.1.0 /opt/node

Add that node to the path, then check the version
> node --version && npm --version
v5.1.0
3.3.12

On ARMV71
> wget https://nodejs.org/dist/v5.1.0/node-v5.1.0-linux-armv7l.tar.gz

Same thing as On ARMV61
> node --version && npm --version
v5.1.0
3.3.12

2 Error Handling

For example:
/*jslint node: true */
'use strict';

var ContextIO = require('contextio');
var HashMap = require('hashmap').HashMap;
var _ = require('../underscore');

var services = null;
var cxtioClient = null;
var jobsFilter = null;
var resumesFilter = null;

module.exports = {
    init: init,
    fetchEmailsWithFilter: fetchEmailsWithFilter,
    fetchAttachmentLink: fetchAttachmentLink,
    fetchMailContent: fetchMailContent,
    fetchResumesByAccountCode: fetchResumesByAccountCode,
    fetchJobsByAccountCode: fetchJobsByAccountCode,
    fetchAccountInfoByEmail: fetchAccountInfoByEmail
};

function init(myServices) {
    services = myServices;

    var logger = services.get('logger');
    var config = services.get('config');

    logger.info("EmailScan - initConnection ---------------");
    logger.info(config.data.contextio);

    cxtioClient = new ContextIO.Client(config.data.contextio.version, config.data.contextio.endpoint, config.data.contextio.security);
    jobsFilter = config.data.contextio.jobsFilter;
    resumesFilter = config.data.contextio.resumesFilter;
    //todo: log this twice?
    logger.info("EmailScan - initConnection ---------------");
}

function fetchEmailsWithFilter(accountCode, filter, callback) {
    var logger = services.get('logger');

    logger.trace("EmailScan - fetchEmailsWithFilter - accountCode = " + accountCode);
    logger.trace("EmailScan - fetchEmailsWithFilter - filter = " + JSON.stringify(filter));

    cxtioClient
        .accounts(accountCode)
        .messages()
        .get(filter, function (err, data) {
            if (err) {
                logger.error("EmailScan - fetchEmailsWithFilter - fail to fetch the messages.");
                callback(err, []);
                return;
            }

            if (!data) {
                var warning_message = "EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter);
                logger.warn(warning_message);
                callback(null, []);
            }

            if (data.statusCode !== 200) {
                handleNotOk(data, logger, callback);
                return;
            }

            var msgBody = data.body;
            logger.trace("EmailScan - fetchEmailsWithFilter - get the messages info with filter = " + JSON.stringify(filter));
            logger.trace("EmailScan - fetchEmailsWithFilter - get response info = " + JSON.stringify(data));
            logger.trace("EmailScan - fetchEmailsWithFilter - get message body = " + JSON.stringify(msgBody));

            if (msgBody.length < 1) {
                var warning_msg = "EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter);
                logger.warn(warning_msg);
                callback(null, []);
                return;
            }

            callback(null, msgBody);
        });
}

function handleNotOk (data, logger, callback) {
    //error handle, handle the error
    logger.error("Unsuccessful response handle " + JSON.stringify(data));
    var statusCode = 500;
    if (data.statusCode !== undefined) {
        statusCode = data.statusCode;
    }
    var err_obj = {"statusCode": statusCode, "message": data.body};
    callback(err_obj, null);
}

I like the idea that my colleague change the exports part. Error Handling on the express.io controller
/*jslint node: true */
'use strict';

var HashMap = require('hashmap').HashMap;

var    services = null;

module.exports = {

    getAccountByEmail : function(req, res) {
        //services
        var emailscan = services.get('emailscan');
        var logger = services.get('logger');

        //params
        var email = req.params.email;

        emailscan.fetchAccountInfoByEmail(email, function(err, account){
            if(err){
                res.status(err.statusCode).json(err.message);
                return ;
            }

            if(account === undefined || account === null){
                res.status(404).json({ "type":"error", "value":"The email[" + email + "] is not valid!"});
                return ;
            }

            logger.debug("account info = " + account.id);
            res.json(account);
            return ;
        });
    },

    init : function(myservices){
        services = myservices;
    }

};

References:
http://sillycat.iteye.com/blog/2256088

你可能感兴趣的:(NodeJS Version Upgrade 5.1.0 and Error Handling)