Node.js –to AngularJS Stack components
With the basic structure of the web
framework fresh in your mind, it is time to discuss the Node.js-to-AngularJS
stack.
The most common—and I believe the best—version
of this stack is the Node.js-to-AngularJS stack comprised of MongoDB, Express,
AngularJS, and Node.js.
In the Node.js-to-AngularJS stack,
Node.js provides the fundamental platform for development. The backend services
and server-side scripts are all written in Node.js. MongoDB provides the data
store for the website but is accessed via a MongoDB driver Node.js module. The
webserver is defined by Express, which is also a Node.js module.
The view in the browser is defined and
controlled using the AngularJS framework. AngularJS is an MVC framework in
which the model is made up of JSON or JavaScript objects, the view is HTML/CSS,
and the controller is AngularJS JavaScript code.
Figure 1.2 provides a
very basic diagram of how the Node.js to AngularJS stack fits into the basic
website/web application model. The following sections describe each of these
technologies and why they were chosen as part of the Node.js to AngularJS
stack.
Figure 1.2 Basic diagram
showing where Node.js, Express, MongoDB, and AngularJS fit in the web paradigm.
Node.js
Node.js is a development
framework that is based on Google’s V8 JavaScript engine and executes it.
You can write most—or
maybe even all—of your server-side code in Node.js, including the webserver and
the server-side scripts and any supporting web application functionality. The
fact that the webserver and the supporting web application scripts are running
together in the same server-side application allows for much tighter
integration between the webserver and the scripts. Also, the webserver can run
directly on the Node.js platform as a Node.js module, which means it’s much
easier than using, say, Apache for wiring up new services or server-side
scripts.
The following are just a
few reasons Node.js is a great framework:
·
JavaScript end-to-end: One of the biggest advantages of Node.js
is that it allows you to write both server- and client-side scripts in
JavaScript. There have always been difficulties in deciding whether to put
logic in client-side scripts or server-side scripts. With Node.js you can take
JavaScript written on the client and easily adapt it for the server and vice
versa. An added plus is that client developers and server developers are
speaking the same language.
·
Event-driven scalability: Node.js applies a unique logic to handling
web requests. Rather than having multiple threads waiting to process web
requests, with Node.js they are processed on the same thread, using a basic
event model. This allows Node.js webservers to scale in ways that traditional
webservers can’t.
·
Extensibility: Node.js has a great following and very active development
community. People are providing new modules to extend Node.js functionality all
the time. Also, it is very simple to install and include new modules in
Node.js; you can extend a Node.js project to include new functionality in
minutes.
·
Fast implementation: Setting up Node.js and developing in it
are super easy. In only a few minutes you can install Node.js and have a
working webserver.
MongoDB
MongoDB is an agile and
very scalable NoSQL database. The name Mongo comes from the word “humongous,” emphasizing the
scalability and performance MongoDB provides. It is based on the NoSQL document
store model, which means data is stored in the database as basically JSON
objects rather than as the traditional columns and rows of a relational
database.
MongoDB provides great
website backend storage for high-traffic websites that need to store data such
as user comments, blogs, or other items because it is quickly scalable and easy
to implement. This book covers using the MongoDB driver library to access
MongoDB from Node.js.
Node.js supports a
variety of database access drivers, so the data store can easily be MySQL or
some other database. However, the following are some of the reasons that
MongoDB really fits in the Node.js stack well:
·
Document orientation: Because MongoDB is document oriented, data
is stored in the database in a format that is very close to what you deal with
in both server-side and client-side scripts. This eliminates the need to
transfer data from rows to objects and back.
·
High performance: MongoDB is one of the highest-performing databases
available. Especially today, with more and more people interacting with
websites, it is important to have a backend that can support heavy traffic.
·
High availability: MongoDB’s replication model makes it very
easy to maintain scalability while keeping high performance.
·
High scalability: MongoDB’s structure makes it easy to scale
horizontally by sharding the data across multiple servers.
Express
The Express module acts
as the webserver in the Node.js-to-AngularJS stack. Because it runs in Node.js,
it is easy to configure, implement, and control. The Express module extends
Node.js to provide several key components for handling web requests. It allows
you to implement a running webserver in Node.js with only a few lines of code.
For example, the Express
module provides the ability to easily set up destination routes (URLs) for
users to connect to. It also provides great functionality in terms of working
with HTTP request and response objects, including things like cookies and HTTP
headers.
The following is a
partial list of the valuable features of Express:
·
Route management: Express makes it easy to define routes
(URL endpoints) that tie directly to the Node.js script functionality on the
server.
·
Error handling: Express provides built-in error handling for “document not
found” and other errors.
·
Easy integration: An Express server can easily be
implemented behind an existing reverse proxy system, such as Nginx or Varnish.
This allows you to easily integrate it into your existing secured system.
·
Cookies: Express provides easy cookie management.
·
Session and cache management: Express also
enables session management and cache management.
AngularJS
AngularJS is a
client-side framework developed by Google. It provides all the functionality
needed to handle user input in the browser, manipulate data on the client side,
and control how elements are displayed in the browser view. It is written in
JavaScript, with a reduced jQuery library. The theory behind AngularJS is to
provide a framework that makes it easy to implement web applications using the
MVC framework.
Other JavaScript
frameworks could be used with the Node.js platform, such as Backbone, Ember,
and Meteor. However, AngularJS has the best design, feature set, and trajectory
at this writing. Here are some of the benefits AngularJS provides:
·
Data binding: AngularJS has a very clean method for binding data to HTML
elements, using its powerful scope mechanism.
·
Extensibility: The AngularJS architecture allows you to easily extend
almost every aspect of the language to provide your own custom implementations.
·
Clean: AngularJS forces you to write clean, logical code.
·
Reusable code: The combination of extensibility and clean code makes it
very easy to write reusable code in AngularJS. In fact, the language often forces
you to do so when creating custom services.
·
Support: Google is investing a lot into this project, which gives it
an advantage over similar initiatives that have failed.
·
Compatibility: AngularJS is based on JavaScript and has a close
relationship with jQuery. This makes it easier to begin integrating AngularJS
into your environment and reuse pieces of your existing code within the
structure of the AngularJS framework.
No comments:
Post a Comment