Showing posts with label Node.js. Show all posts
Showing posts with label Node.js. Show all posts

Tuesday, February 05, 2013

Nodejs and compression of HTML, JS, CSS

To decrease loading time for pages, it is always nice to minify your HTML, CSS and JS. Also to decrease number of requests we inject our minified CSS/JS directly on our pages (ofc we will do that unless our CSS/JS have small size), so minus 2 requests to all pages.

Now time to write couple lines of real code

1. UglifyJS - compress JS

var uglifyJS = require("uglify-js");

var result = uglifyJS.minify("filename.js");
console.log(result.code);

2. Sqwish - compress CSS

var sqwish = require('sqwish');
var filename=app.get('filename.css');
fs.readFile(filename, 'utf8', function(err, data) {
    if (err) throw err;
    var minifiedCss = sqwish.minify(data)
    console.log(minifiedCss);
})

3. HTMLMinifier - compress HTML

var htmlminifier = require('html-minifier');
var minifiedHTML = htmlminifier.minify(data, {
 removeComments: true,
 removeCommentsFromCDATA: true,
 collapseWhitespace: true,
 collapseBooleanAttributes: true,
 removeAttributeQuotes: true,
 removeEmptyAttributes: true
});
So at the result we decreased size of our pages a lot and also decreased number of requests which is also nice, we all love "fast pages".

Wednesday, January 30, 2013

Node.js first experience and first simple project

From 2013 I've started to learn (as much as I've free time) Node.js. 'Domino/Notes' still my main, however it is always nice to learn something new.

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast,
scalable network applications. Node.js uses an event-driven, non-blocking I/O model 
that makes it lightweight and efficient, perfect for data-intensive real-time 
applications that run across distributed devices.

Together with Andrew Kuba (who guide me how to 'node') we are doing simple website for one game which is very popular right now, we are aiming to have hundreds users/month :) at some points + it is always nice to do something that is real and online
We are using only couple modules for our first application:
  • Express - minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
  • Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects
  • mustache is a logic-less templates.
  • yuicompressor compressor to minify our JS/CSS (we do compress all our CSS/JS and put it directly on page to avoid additional lookups)
  • html-minifier compressor to minify our HTML
We do not use database right now, however we are planning to use mongodb with mongoose in near future.

Pick LoL is our website (first version) we are working on to get some experience with Node.js

During next couple months I will write more posts about node.js which I'm going to like hopefully :).