Archive for the ‘Node.js’ Category

A ToDo Website based on Node.js, Express and Jscex

Friday, March 9th, 2012

The most seen context of Jscex is “JavaScript Asynchronous Programming”, and it can be used on both the browser and the server. Recently, Node.js is very popular, and the original Windows version has just been released, and many students are using it to make websites and something like that. The most famous framework used by Node.js based websites is Express, which is easy to use.

JavaScript is a kind of language with no blocking characteristics, so all kinds of APIs are designed to be asynchronous, which is good to both the scalability of the server and the response ability of a client page. But there will be some problems when programming. For example, it is a ToDo demonstration of a simple handler, because the data base needs to be checked, so there should be callbacks:

1. exports.index = function (req, res, next) { 

2.     db.query

(‘select * from todo order by finished asc, id asc limit 50′, function 

(err, rows) { 

3. if (err) return next(err); 

4.         res.render(‘index’, { todos: rows }); 

5.     }); 

6. };

The db variable is used to operate the MySQL data base. Its query method introduces SQLs (maybe some indexes) and provides a callback function to suggest errors or return to the search results. In the callback, we must be clear that whether there is an error. If there is, we have to use the “next” framework to report “error”. We should do this in every asynchronous operation step. If there is another check after this check, we must do another error checking. Every handler requires this, it is also something annoying about asynchronous programming: it is hard to do an overall exception handling, and the handlers should be placed everywhere. It will turn something abnormal, which will be hard to find out.

I used the Jscex to modifify the ToDo website. At first, I enable the inquiry of MySQL to access.

Jscex(lib\jscex.mysql.js):

1. exports.jscexify = function (db) { 

2.     db.queryAsync = function () { 

3. var _this = this

4.

5. var args = []; 

6. for (var i = 0; i < arguments.length; i++) { 

7.             args.push(arguments[i]); 

8.         } 

9.

10. var delegate = { 

11.             onStart: function (callback) { 

12.

13.                 args.push(function (err, result) { 

14. if (err) { 

15.                         callback("failure", err); 

16.                     } else

17.                         callback("success", result); 

18.                     } 

19.                 }); 

20.

21.                 _this.query.apply(_this, args); 

22.             } 

23.         }; 

24.

25. return new Jscex.Async.Task(delegate); 

26.     } 

27. }

Generally speaking, not so many codes are required to modify an asynchronous interface with Jscex (what is the most important is just the onStart function). There are nearly 30 lines of codes, but most of them are there to support the elongated indexes, so the query Async function will retain all the indexes being called, and complete it with a callback, then call the query function itself. Now, you can change the indexed and other handlers (controller\todo.js), for example:

1. exports.index = toHandler(eval(Jscex.compile("async", function (req, res) { 

2.

3. var todos = $await(db.queryAsync(‘select * from todo order by finished asc, id asc limit 50′)); 

4.     res.render("index", { todos: todos }); 

5.

6. })));

The toHandler function is a handler turning a “receiving req and res, and returning to Task” function to a standard “receiving req, res and next” handlers, dealing with errors al-together.

1. var toHandler = function (asyncFunc) { 

2. return function (req, res, next) { 

3. var task = asyncFunc(req, res); 

4.         task.addListener(function () { 

5. if (task.status == "failed") { 

6.                 next(task.error); 

7.             } 

8.         }); 

9.         task.start(); 

10.     } 

11. }

ToDo websites are based on Express, ejs and MySQL, and I add Jscex as its sub-module. If you are going to put some ToDo codes in it, you can:

1. > git clone git://github.com/JeffreyZhao/todo.git

2. > cd todo 

3. > git submodule init 

4. > git submodule update 

5. > npm install express ejs mysql 

6. > node server.js

You may still be confused at the present. But after acquiring some basic knowledge about Jscex and reading my article, you will find that it is really a non-traditional way.

Did you like this? Share it:

Hands-on with Node.js support in Komodo IDE 7

Tuesday, February 7th, 2012

ActiveState has released a major new version of the Komodo integrated development environment (IDE). The update, which is called Komodo 7, introduces several useful new features and support for additional programming languages.

Komodo is a high-end commercial development tool for programmers who work with scripting languages such as Python and Ruby. It’s especially well-suited for developing large-scale Web applications. It supports code completion and breakpoint debugging for a relatively broad number of programming languages.

Komodo is built on top of Mozilla’s Gecko rendering engine with the XUL user interface toolkit. Some of Komodo’s underlying architecture is inherited from Firefox, including a rich add-on system. The core editing components of Komodo IDE are developed as open source software and are shared with a free standalone editor called Komodo Edit that lacks the IDE’s debugger and other advanced functionality.

Some of the major new features introduced in Komodo 7 include support for real-time collaborative editing on code files, a new built-in profiling tool for Python and PHP, a much-improved syntax checker, and the ability to synchronize your settings between computers. Alongside these new features, Komodo 7 also adds support for several new programming languages and frameworks, including CoffeeScript and Node.js.

I haven’t had a chance yet to try all of the new features, but I put the new Node.js functionality to work during some hands-on testing. Komodo’s Node.js support isn’t as mature or robust as its handling of other languages yet, but it still proved useful for Node.js development.

The built-in debugging support for Node.js is fairly comprehensive and well done. In addition to showing you the value of your watch expressions when you hit a breakpoint, it will also show you a tree of all the variables within the current scope. You can expand data structures and objects to see their contents. When you use the debugger to step into an expression, the IDE can find and display the function you have stepped into even if it is in another module.

Komodo’s newly improved syntax checker is useful and very functional. It picked up errors in my JavaScript code, such as unmatched parentheses and missing operators, almost instantly. It will tell you the line and column and take you to the right place when you click the entry in the error panel.

Komodo’s code browser, which gives you a visual overview of the functions, objects, and variables defined in the currently active file, works relatively well with JavaScript. It’s not as useful as it could be for Node.js development, however.

Due to the event-based nature of the framework, a lot of code in Node.js applications tends to be implemented inside of callbacks. This means that you end up with a lot of indistinguishable anonymous functions in the Komodo code browser. In cases where you have an anonymous function bound to an EventEmitter signal, I’d prefer to have the code browser show the event name. That issue aside, the code browser is still quite good for Node.js programming when you are working with more conventional JavaScript.

Read More:

http://arstechnica.com/business/news/2012/02/hands-on-testing-nodejs-support-in-komodo-ide-7.ars?utm_source=rss&utm_medium=rss&utm_campaign=rss

Did you like this? Share it:

Node.js Selected by InfoWorld for 2012 Technology of the Year Award

Tuesday, February 7th, 2012

Joyent, a global provider of cloud computing software and services, today announced that Node.js, an open source platform for easily building fast, scalable real time applications, has been chosen by InfoWorld for the Technology of the Year Award. Joyent is the corporate sponsor and key driver of Node.js and offers an integrated suite of products and services utilizing this award winning technology. The platform is ideally suited to run on Joyent SmartOS, the Joyent open source operating system delivering real time analytics and leading process and container security designed to stand up to the growing performance and scalability demands of modern computing.

Selected by InfoWorld Test Center editors and reviewers, the annual awards identify the best and most innovative products on the IT landscape. Winners are drawn from all of the products tested during the past year, with the final selections made by InfoWorld’s Test Center staff. All products reviewed by the Test Center are eligible to win.

Node.js uses event-driven, asynchronous I/O that minimizes overhead and maximizes scalability. This makes Node.js an ideal platform for data intensive real-time (DIRTy) applications that include:

[…]

Read More:

http://www.marketwatch.com/story/nodejs-selected-by-infoworld-for-2012-technology-of-the-year-award-2012-01-11

Did you like this? Share it:

Cloud9 launches documentation site to support growing Node.js community

Tuesday, February 7th, 2012
Cloud9 on Node.js

We discussed the evolution of the JavaScript programming language, the advantages of server-side JavaScript development, and the benefits of Node.js. I started by asking Daniels if improvements to the standard have made JavaScript a competitive language for large-scale development. He said that it’s competitive today and is advancing in a direction that will continue to improve its efficacy for building server-side software.

"JavaScript marries the concepts of a functional and imperative language in a way that makes it very easy for people to start using it," he said. "Already, it is a real competitor. The biggest hurdle right now is going from a popular language for building client-side apps to building server-side enterprise applications. The way the language is evolving is supporting that."

One area where he sees the need for improvement in JavaScript is the lack of a native module system. Node.js uses the CommonJS module system, but it would be advantageous to have one that is inherent to the language and consistent between various JavaScript runtimes.

Daniels characterizes the ability for developers to reuse existing code and skills between the client and the server as a major advantage of using Node.js to build web applications. When I asked if the differences between JavaScript engines and the lack of certain features on the browser side posed challenges for reusability, he explained that portable JavaScript libraries help insulate developers from those kinds of issues. In some cases, he said, it’s simply a matter of being mindful when you write code about where you else you might want to use it.

We also talked about the applicability of Node.js. The framework really shines for building scalable real-time applications, but Daniels says that it can be used for much more than that.

"The use case is much broader than just the real-time Web," Daniels told Ars. "Almost every Web application has a services based architecture. The moment you have that, Node.js is suited for it."

Cloud9 recently hired Tim Caswell, Bert Belder and Ben Noordhuis, three prominent developers from the Node.js community. One of the ways in which Cloud9 is putting their expertise to use is by launching a pair of websites that can serve as informational resources for the broader Node.js community.

One of those sites, which is called Nodebits, is a blog that publishes sample code, discusses best practices, and highlights ways that developers can get more out of Node.js. The other site, which is called Nodemanual, aims to offer comprehensive reference documentation and introductory tutorials. As the domain name suggests, the latter will become the official location of the Node.js manual.

Read More:

http://arstechnica.com/business/news/2012/01/cloud9-launches-documentation-site-to-support-growing-nodejs-community.ars?utm_source=rss&utm_medium=rss&utm_campaign=rss

Did you like this? Share it:

Node.js Web Service

Tuesday, February 7th, 2012

- Why We Should Pay Attention to Node.js

Node.js

Node.js is a platform designed for building scalable network applications on Chrome’s JavaScript runtime (V8: Google’s open source JavaScript engine). Programs are written in JavaScript that is fit for writing servers due to its event-driven nature. Developers not only get benefit from the speed of V8, but also Node.js/JavaScript makes developers write code that is fast by design.

[…]

Read More:

http://www.techomechina.com/node-js-web-service/

Did you like this? Share it: