Node js Blueprints Tsonev instant download
Node js Blueprints Tsonev instant download
https://ebookname.com/product/node-js-blueprints-tsonev/
https://ebookname.com/product/node-js-by-example-1st-edition-
tsonev/
https://ebookname.com/product/node-js-in-practice-1st-edition-
alex-r-young/
https://ebookname.com/product/ext-js-application-development-
blueprints-1st-edition-ramsay/
https://ebookname.com/product/the-sociology-of-gender-and-the-
family-middle-class-families-1st-edition-colin-bell/
Macromedia Dreamweaver MX Fast Easy Web Development 1st
Edition Aneesha Bakharia
https://ebookname.com/product/macromedia-dreamweaver-mx-fast-
easy-web-development-1st-edition-aneesha-bakharia/
https://ebookname.com/product/words-made-flesh-essays-honoring-
kenan-b-osborne-o-f-m-1st-edition-joseph-chinnici/
https://ebookname.com/product/surface-coating-and-modification-
of-metallic-biomaterials-1st-edition-cuie-wen/
https://ebookname.com/product/busn-4-4th-edition-marcella-kelly/
https://ebookname.com/product/nine-nasty-words-14th-edition-john-
mcwhorter/
Gateway 2nd Edition Anna Cole
https://ebookname.com/product/gateway-2nd-edition-anna-cole/
Node.js Blueprints
Krasimir Tsonev
BIRMINGHAM - MUMBAI
Node.js Blueprints
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78328-733-8
www.packtpub.com
Bojan currently lives in Germany and works for GMG GmbH & Co. KG, developing
a wide range of cross-platform color management and printing solutions, using both
Microsoft and open-source technologies. In his spare time, he actively participates in
open source projects.
He also has worked on the book Professional Visual Studio 2008, Wrox.
To Carina, my wife.
Leo Hsieh graduated from USF with a Master's degree in Web Science in 2011.
He has been working as a software engineer for over two and a half years. He is
an open-source JavaScript developer, interested in frontend development and
Node.js. Although he is more focused on frontend development, he is able to
work on backend development with Java and Python as well.
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM
http://PacktLib.PacktPub.com
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print and bookmark content
• On demand and accessible via web browser
[ ii ]
Table of Contents
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Table of Contents
[ vi ]
Preface
As you probably know, the big things in our sphere are those that are moved by the
community. Node.js is a technology that has become really popular. Its ecosystem
is well-designed and brings with it the flexibility we need. With the rise of mobile
development, JavaScript occupies a big part of the technology stack nowadays.
The ability to use JavaScript on the server side is really interesting. It's good to know
how Node.js works and where and when to use it, but it is more important to see
some examples. This book will show you how this wonderful technology handles
real use cases.
Chapter 2, Developing a Basic Site with Node.js and Express, discusses how ExpressJS
is one of the top frameworks on the market. ExpressJS was included because of its
fundamental importance in the Node.js world. At the end of the chapter, you will
be able to create applications using the built-in Express modules and also add your
own modules.
Chapter 3, Writing a Blog Application with Node.js and AngularJS, teaches you how to
use frontend frameworks such as AngularJS with Node.js. The chapter's example is
actually a dynamic application that works with real databases.
Chapter 4, Developing a Chat with Socket.IO, explains that nowadays, every big web
app uses real-time data. It's important to show instant results to the users. This
chapter covers the creation of a simple real-time chat. The same concept can be used
to create an automatically updatable HTML component.
Preface
Chapter 5, Creating a To-do Application with Backbone.js, illustrates that Backbone.js was
one of the first frameworks that introduced data binding at the frontend of applications.
This chapter will show you how the library works. The to-do app is a simple example,
but perfectly illustrates how powerful the framework is.
Chapter 6, Using Node.js as a Command-line Tool, covers the creation of a simple CLI
program. There are a bunch of command-line tools written in Node.js, and the
ability to create your own tool is quite satisfying. This part of the book will present
a simple application which grabs all the images in a directory and uploads them
to Flickr.
Chapter 7, Showing a Social Feed with Ember.js, describes an Ember.js example that will
read a Twitter feed and display the latest posts. That's actually a common task of
every developer because a lot of applications need to visualize social activity.
Chapter 8, Developing Web App Workflow with Grunt and Gulp, shows that there are
a bunch of things to do before you can deliver the application to the users, such as
concatenation, minification, templating, and so on. Grunt is the de facto standard
for such tasks. The described module optimizes and speeds up your workflow. The
chapter presents a simple application setup, including managing JavaScript, CSS,
HTML, and cache manifests.
Chapter 9, Automate Your Testing with Node.js, signifies that tests are really important
for every application nowadays. Node.js has some really great modules for this. If
you are a fan of test-driven development, this chapter is for you.
Chapter 10, Writing Flexible and Modular CSS, introduces the fact that two of the
most popular CSS preprocessors are written in Node.js. This chapter is like a little
presentation on them and, of course, describes styling a simple web page.
Chapter 11, Writing a REST API, states that Node.js is a fast-working technology, and
it is the perfect candidate for building a REST API. You will learn how to create a
simple API to store and retrieve data for books, that is, an online library.
Chapter 12, Developing Desktop Apps with Node.js, shows that Node.js is not just a web
technology—you can also create desktop apps with it. It's really interesting to know
that you can use HTML, CSS, and JavaScript to create desktop programs. Creating a
simple file browser may not be such a challenging task, but it will give you enough
knowledge to build your own applications.
[2]
Preface
Conventions
In this book, you will find a number of styles of text that distinguish between
different kinds of information. Here are some examples of these styles, and an
explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"The http module, which we initialize on the first line, is needed for running the
web server."
New terms and important words are shown in bold. Words that you see on the
screen, in menus or dialog boxes for example, appear in the text like this: "Click on
the blue button with the text OK, I'LL AUTHORIZE IT."
[3]
Preface
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about
this book—what you liked or may have disliked. Reader feedback is important for us
to develop titles that you really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to
help you to get the most from your purchase.
[4]
Preface
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you find a mistake in one of our books—maybe a mistake in the text or
the code—we would be grateful if you would report this to us. By doing so, you can
save other readers from frustration and help us improve subsequent versions of this
book. If you find any errata, please report them by visiting http://www.packtpub.
com/submit-errata, selecting your book, clicking on the errata submission form link,
and entering the details of your errata. Once your errata are verified, your submission
will be accepted and the errata will be uploaded on our website, or added to any list of
existing errata, under the Errata section of that title. Any existing errata can be viewed
by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media.
At Packt, we take the protection of our copyright and licenses very seriously. If you
come across any illegal copies of our works, in any form, on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors, and our ability to bring you
valuable content.
Questions
You can contact us at questions@packtpub.com if you are having a problem with
any aspect of the book, and we will do our best to address it.
[5]
Common Programming
Paradigms
Node.js is a JavaScript-driven technology. The language has been in development for
more than 15 years, and it was first used in Netscape. Over the years, they've found
interesting and useful design patterns, which will be of use to us in this book. All this
knowledge is now available to Node.js coders. Of course, there are some differences
because we are running the code in different environments, but we are still able
to apply all these good practices, techniques, and paradigms. I always say that it
is important to have a good basis to your applications. No matter how big your
application is, it should rely on flexible and well-tested code. The chapter contains
proven solutions that guarantee you a good starting point. Knowing design patterns
doesn't make you a better developer because in some cases, applying the principles
strictly won't work. What you actually get is ideas, which will help you in thinking
out of the box. Sometimes, programming is all about managing complexity. We all
meet problems, and the key to a well-written application is to find the best suitable
solutions. The more paradigms we know, the easier our work is because we have
proven concepts that are ready to be applied. That's why this book starts with an
introduction to the most common programming paradigms.
Common Programming Paradigms
Node.js fundamentals
Node.js is a single-threaded technology. This means that every request is processed
in only one thread. In other languages, for example, Java, the web server instantiates
a new thread for every request. However, Node.js is meant to use asynchronous
processing, and there is a theory that doing this in a single thread could bring good
performance. The problem of the single-threaded applications is the blocking I/O
operations; for example, when we need to read a file from the hard disk to respond to
the client. Once a new request lands on our server, we open the file and start reading
from it. The problem occurs when another request is generated, and the application is
still processing the first one. Let's elucidate the issue with the following example:
var http = require('http');
var getTime = function() {
var d = new Date();
return d.getHours() + ':' + d.getMinutes() + ':' +
d.getSeconds() + ':' + d.getMilliseconds();
}
var respond = function(res, str) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(str + '\n');
console.log(str + ' ' + getTime());
}
var handleRequest = function (req, res) {
console.log('new request: ' + req.url + ' - ' + getTime());
if(req.url == '/immediately') {
respond(res, 'A');
} else {
var now = new Date().getTime();
while(new Date().getTime() < now + 5000) {
// synchronous reading of the file
}
respond(res, 'B');
}
}
http.createServer(handleRequest).listen(9000, '127.0.0.1');
[8]
Another Random Document on
Scribd Without Any Related Topics
1842, se sont écoulés depuis que La Moricière est installé à Mascara,
sa division en a passé trois cent dix en campagne.
XI
XII
XIV
XV
XVI
Tout occupé qu'il fût de ce qui se passait sous ses yeux en Afrique,
le général Bugeaud suivait de loin, avec une attention passionnée,
les péripéties de la question algérienne en France. Il ne se contentait
pas d'y exercer une action indirecte, mais décisive, par ses succès
mêmes, qui enhardissaient les partisans de la colonie, décidaient les
hésitants, désarmaient ou discréditaient les adversaires. Il prétendait
y intervenir d'une façon plus directe; comme l'a dit M. Guizot, «il se
croyait engagé, à la fois, sur deux champs de bataille, sur celui de la
discussion publique à la tribune ou dans la presse, en France, aussi
bien que sur celui de la guerre, en Afrique, et il voulait, en toute
occasion, faire acte de présence et de vaillance sur les deux».
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com