Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Node.js By Example
Node.js By Example

Node.js By Example: Learn to use Node.js by creating a fully functional social network

eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Node.js By Example

Chapter 2. Architecting the Project

Software development is a complex process. We can't just start writing some code and expect that we will reach our goal. We need to plan and define the base of our application. In other words, before you dive into actual scripting, you have to architect the project. In this chapter, we will cover the following:

  • The basic layers of a Node.js application
  • Using the task runner and building system
  • Test-driven development
  • The Model-View-Controller pattern
  • The REST API concept

Introducing the basic layers of the application

If we plan to build a house, we will probably want to start with a very good base. We simply can't build the first and second floor if the base of the building is not solid.

However, with software, it is a bit different. We can start developing code without the existence of a good base. We call this brute-force-driven development. In this, we produce feature after feature without actually caring about the quality of our code. The result may work in the beginning, but in the long term, it consumes more time and probably money. It's well-known that software is nothing but building blocks placed on top of one another. If the lower layers of our program are poorly designed, then the whole solution will suffer because of this.

Let's think about our project—the social network that we want to build with Node.js. We start with a simple code like this one:

var http = require('http');
http.createServer(function (req, res...

The task runner and building system

Along with the practice of running the Node.js server, there are other best practices pertaining to web development tasks that you can consider. We are building a web application. So, we have client-side JavaScript and CSS that has to be delivered in the best possible way. In other words, to increase the performance of our website, we need to merge all the JavaScript into a single file and compress it. The same is valid for the CSS style sheets. If you do this, the browser will make fewer requests to the server.

Node.js is a common tool for command-line utilities, except for when you want to run web servers. There are many modules available for the packaging and optimizing of assets. It is great that there are task runners and build systems that help you manage these processes.

Introducing Grunt

Grunt is one of the most popular task runners that are based on Node.js. It is available in the package manager registry and can be installed by using the following...

Discovering Gulp

Gulp is a build system that automates common tasks. As in Grunt, we can compose our asset pipeline. However, there are a few differences between the two:

  • We still have a configuration file, but it is called gulpfile.js.
  • Gulp is a streaming-based tool. It doesn't store anything on the disc when it is working. Grunt needs to create temporary files in order to pass data from one task to another, but Gulp keeps the data in the memory.
  • Gulp follows the code-over-configuration principle. In the gulpfile.js file, we write our tasks like a regular Node.js script. We will see a demonstration of this in a minute.

To use Gulp, we have to install it first. The following command will set up the tool globally:

npm install -g gulp

We are going to use a few plugins—gulp-concat, gulp-uglify, and gulp-rename. After adding them to our package.json file, run npm install so that we can install them.

The next step is to create a new gulpfile.js file in the root directory of our project...

Test-driven development

Test-driven development is a software development process in which automated tests drive the development cycle of a new product or functionality. It speeds up the development in the long run and tends to produce better code. Nowadays, many frameworks have tools that help you create automated tests. So as developers, we need to write and run tests first before writing any new code. We always check what the result of our work is. In web development, we usually open the browser and interact with our application to see how our code behaves. So, a major part of our time goes into testing. The good news is that we may optimize this process. It is possible to write code that does the job instead of us. Sometimes, relying on manual testing is not the best option, because it takes time. Here are a few benefits of having tests:

  • The tests improve the stability of our application
  • Automated testing saves time that can be spent in improving or refactoring the system's code
  • Test...

The Model-View-Controller pattern

It's always difficult to start a new project or the implementation of a new feature. We just don't know how to structure our code, what modules to write, and how they are going to communicate. In such cases, we often trust well-known practices—design patterns. Design patterns are reusable solutions to commonly occurring problems. For example, the Model-View-Controller pattern has proven to be one of the most effective patterns for web development due to its clear separation of the data, logic, and presentation layers. We will base our social network on a variation of this pattern. The traditional parts and their responsibilities are as follows:

The Model-View-Controller pattern
  • Model: The Model is the part that stores the data or the state. It triggers an update on the View once there is a change.
  • View: The View is usually the part that the user can see. It is a direct representation of the data or the state of the Model.
  • Controller: The user interacts with the help of the...

Introducing the basic layers of the application


If we plan to build a house, we will probably want to start with a very good base. We simply can't build the first and second floor if the base of the building is not solid.

However, with software, it is a bit different. We can start developing code without the existence of a good base. We call this brute-force-driven development. In this, we produce feature after feature without actually caring about the quality of our code. The result may work in the beginning, but in the long term, it consumes more time and probably money. It's well-known that software is nothing but building blocks placed on top of one another. If the lower layers of our program are poorly designed, then the whole solution will suffer because of this.

Let's think about our project—the social network that we want to build with Node.js. We start with a simple code like this one:

var http = require('http');
http.createServer(function (req, res) {
   res.writeHead(200, {'Content...

The task runner and building system


Along with the practice of running the Node.js server, there are other best practices pertaining to web development tasks that you can consider. We are building a web application. So, we have client-side JavaScript and CSS that has to be delivered in the best possible way. In other words, to increase the performance of our website, we need to merge all the JavaScript into a single file and compress it. The same is valid for the CSS style sheets. If you do this, the browser will make fewer requests to the server.

Node.js is a common tool for command-line utilities, except for when you want to run web servers. There are many modules available for the packaging and optimizing of assets. It is great that there are task runners and build systems that help you manage these processes.

Introducing Grunt

Grunt is one of the most popular task runners that are based on Node.js. It is available in the package manager registry and can be installed by using the following...

Left arrow icon Right arrow icon
Download code icon Download Code

Description

If you are a JavaScript developer with no experience with Node.js or server-side web development, this book is for you. It will lead you through creating a fairly complex social network. You will learn how to work with a database and create real-time communication channels.

Who is this book for?

If you are a JavaScript developer with no experience with Node.js or server-side web development, this book is for you. It will lead you through creating a fairly complex social network. You will learn how to work with a database and create real-time communication channels.

What you will learn

  • Get to know the fundamentals of Node.js
  • Understand why architecting is important and what the planning of a typical Node.js application looks like
  • Successfully manage every web application asset such as CSS, JavaScript, or image files
  • Implement the ModelViewController pattern in the context of a Node.js application
  • Communicate with a database, including storing, retrieving, and deleting data
  • Implement more complex features such as creating social network pages, tagging, sharing, and liking posts
  • Create a realtime chat capability for users of the social network
  • Explore how to test the user interface of your web application

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 25, 2015
Length: 220 pages
Edition : 1st
Language : English
ISBN-13 : 9781784399603
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 25, 2015
Length: 220 pages
Edition : 1st
Language : English
ISBN-13 : 9781784399603
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 147.97
Meteor Cookbook
$43.99
Node.js Design Patterns
$54.99
Node.js By Example
$48.99
Total $ 147.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. Node.js Fundamentals Chevron down icon Chevron up icon
2. Architecting the Project Chevron down icon Chevron up icon
3. Managing Assets Chevron down icon Chevron up icon
4. Developing the Model-View-Controller Layers Chevron down icon Chevron up icon
5. Managing Users Chevron down icon Chevron up icon
6. Adding Friendship Capabilities Chevron down icon Chevron up icon
7. Posting Content Chevron down icon Chevron up icon
8. Creating Pages and Events Chevron down icon Chevron up icon
9. Tagging, Sharing, and Liking Chevron down icon Chevron up icon
10. Adding Real-time Chat Chevron down icon Chevron up icon
11. Testing the User Interface Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.5
(2 Ratings)
5 star 50%
4 star 0%
3 star 0%
2 star 50%
1 star 0%
Alexander Frenkel Aug 06, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great book, for somebody who is starting his first application in Node.JS. it explains in detail how to start your own application, how Node.JS works and simple brings you into the world of Node.JS which is diferent from a standart request/thread application we where used to produce in PHP or ASP.
Amazon Verified review Amazon
Ian M Apr 04, 2016
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The fundamental difference between node and all other popular web languages is the asynchronous approach. This book merely acknowledges this concept and doesn't spend any time explaining the event loop, call stack, or key differences for people who are already well versed in other languages. There is no discussion of promises on the server side and no mention of design patterns specific to the callback-happy world of node.Further, this book spends a lot of time explaining how to do things the hard way. It demonstrates terribly messy page controllers and routers that will quickly grow to unsustainable size without once mentioning industry standard tools such as Express that are meant to avoid these problems. If you follow this book's example, you'll end up with a bloated but well archtected app that wastes lots of code space and is a nightmare to maintain.Finally, the book suffers several small grammatical errors. Normally, I wouldn't note this, but in a technical manual non-parsing sentences can lead to misinterpretation.It is also worth noting that this book does not once mention ES6 or any of the major changes that are now supported.I recommend this book only to people who already understand node well but want a guided approach to solving some core concepts they may have missed such as TDD, restful design, or real-time chat.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.