Componentix logo

Componentix blog

Here we write some random thoughts and articles about software development: Java, Grails, Node.js, iPhone, iPad and more.

Save page to Internet Archive using simple bookmarklet (avoid broken links)

Just drag this link to your browser’s bookmark panel:

Archive

This would allow you to save any page to Internet Archive and cite it using the resulting link, without fear of content changing or disappearing altogether.

Let’s make sure world wide web stays web.

App.net bookmarklet to share the current webpage

Recently I've joined the App.net service, which is basically developer-friendly Twitter.

I couldn’t find any bookmarklet to use for sharing the links on it, so I decided to make one.

So here it is (drag following link to bookmarks panel of your browser). Share on App.net.

Enjoy responsibly :)

UPD: A variant that opens new window (thanks to Amr Eldib) – Share on App.net.

Tags: javascript web

Work with libxmljs DOM as with plain Javascript objects, similar to E4X

Overview

libxmljs-easy is a Node.js module which simplifies XML traversing, similar to E4X.

Installation

npm install libxmljs-easy

Usage

Use module

var easy = require("libxmljs-easy");

Parse XML

var xml = easy.parse('<books><book name="Lord of the Rings">' +
                         '<author name="J. R. R. Tolkien" />' +
                         '<language>English</language>' +
                      '</book></books>');

Select elements from collections explicitly

assert.equal(xml.book[0].$name, "Lord of the Rings");
assert.equal(xml.book[0].author[0].$name, "J. R. R. Tolkien");

Use shorthands (works well for case when there is single child element with given name)

assert.equal(xml.book.$name, "Lord of the Rings");
assert.equal(xml.book.author.$name, "J. R. R. Tolkien");

Basically the idea is that you construct a path from tag names, which can optionally end with attribute name prefixed with “$”.

When index is ommited – the array of elements is matched. When attribute is accessed on such array, its value is concatenated string of attribute values for each of elements in the array.

There is also original DOM element available as “$” property
of individual converted elements.

assert.equal(xml.book.language[0].$.text(), "English");

Further info

File uploads using Node.js: once again

I’ve already wrote an article about file uploads in Node.js before. However Node.js is in rapid development and so many things have changed since then.

So, I decided to do an updated version, which accommodates for such changes:

  • events module was removed
  • posix module was replaced by fs
  • various changes into HTTP request/response API
  • multipart module refactored into separate project
  • convenient fs.createWriteStream instead of low-level API

See the code snippet under the cut, or on github. Special thanks goes to Felix Geisendörfer for some useful suggestions.

Please note that you’ll need to install multipart module for the code to work.

To make it clear, Node.js version used was v. 0.1.91.

UPD: Fixed stream.onData handler. It was writing corrupted data (UTF-8 character were translated) because of binary mode not being used. Thanks to Ron Ward for suggesting solution.

Read more...

File uploads using Node.js: now for real

Introduction

Node.js is a very interesting server-side Javascript application framework. It is based on Google’s V8 Javascript engine and implements evented I/O, so it is blazing fast. It looks very promising for certain types of applications, mostly those that require long-running connections. This includes streaming big files, keeping persistent connections used for realtime communication (chat, games), applications using third-party web services (with long call times), etc.

There is good enough API reference available for Node.js, however it is not trivial to start development as event-based approach is quite unusual compared to commonly used thread-based networking. There are some useful blog posts with code samples available in Debuggable blog, e.g. Streaming file uploads with node.js.

However the given examples have problem — they are too far from real world problems. Take file upload as example — the code sample provided has absolutely no info on how to save the uploaded file. It is assumed that it would be trivial for the reader to figure out, however it’s not when we have to follow evented I/O approach.

In this post I’ll show example of how to save the uploaded files with Node.js. For the impatient — just grab source from github repository.

UPD: The code here is obsolete, see new article about file uploads in new Node.js version.

Read more...

Twitter and Google Maps mashup in 20 minutes with Grails

Introduction

For many developers Java is often a synonym for totally non-sexy enterprise applications development. It is associated with numerous XML configuration files, boilerplate code, etc. So they instead use dynamic languages like Ruby, Python, PHP to develop their projects, especially when these are own simple utilities, mash-ups, etc.

However the Java field has changed much in the recent few years. There are multiple frameworks which relieve developer from “enterprise” burden. Grails is probably one of the best. It is based on Groovy, which is a dynamic language running on Java platform designed specially for Java programmers. It uses well known robust and efficient Java libraries to do all the heavy lifting (Spring, Hibernate, etc.). There is also a plugin system and plugins exist for almost every widely used Java library.

In this article we’ll show how to make a mash-up of Twitter and Google Maps in around 20 minutes. The end result will look similar to this:

Read more...
Following e-mail is only for robots (never send anything to it, or you would be blacklisted): botsonly@componentix.com