STRING DECODER MODULE IN NODE.JS

String Decoder Module in Node.js

STRING DECODER:

String decoder module in node.js is used to provide an API for decoding buffer object into strings. Also, this decoding is performed in a manner that UTF-8 and UTF-16 characters multibyte coding is preserved. The user might query string module in the following way:

var sd = require(‘string_decoder’).StringDecoder;

STRING DECODER METHODS:

String decoder class has two methods which are as follows:

  • STRINGDECODER.WRITE(BUFFER):

This method is used to return the specified buffer as decoded string. The user passes buffer as the argument in this method. Let’s see an example as given below :

//name of the file : write.js
var stringDecoder = require(‘string_decoder’).StringDecoder;
var sd = new stringDecoder(‘utf8′);
var buff = Buffer(‘data to be buffered’);
//Print the buffered data
console.log(buff);
//Print the decoded buffer
console.log(sd.write(buff));

The user can also run it in the following way too:

>node write.js

<Buffer 64 61 74 61 20 74 6f 20 62 65 20 62 75 66 66 65 72 65 64>

data to be buffered

  • STRINGDECODER.END([BUFFER]): 

This method is used to return the remaining of the input stored in internal buffer.

  • buffer <Buffer> | <TypedArray> | <DataView> A Buffer, or TypedArray, or DataView containing the bytes to decode.
  • Returns: <string>

It returns any remaining input stored in the internal buffer as a string. Bytes representing incomplete UTF-8 and UTF-16 characters will be swapped with substitution characters appropriate for the character encoding.

If the buffer argument is provided, one final call to stringDecoder.write() is done before returning the remaining input.

JAVASCRIPT: CALLBACK

JavaScript: Callback

Callback functions is a theory derived from functional programming and specifies the use of functions as arguments.

In JavaScript every function is a first class object, which means that every function is an Object and can be used like any other object. This enables the use of a function as a parameter in another function which is the fundamental idea of callback functions.

PARAMETERS TO CALLBACK FUNCTIONS:

We can also pass parameters to callback functions, just similar to any other function. In the below instance we pass as a parameter the name of the author of the function.

var author = “FF”;
function namedCallback(param){
alert(“namedCallback() called by “+param);
}
function testFunction(callback){
callback();
}
testFunction(namedCallback(author));

MULTIPLE CALLBACK FUNCTION:

Multiple callback functions can be used as parameters of another function.

Var someUlr = …;
function successCallback(){
//success code
}
function completeCallback(){
//complete code
}
function errorCallback(){
//error code
}
$.ajax({
url: someUrl,
success: successCallback,
complete: completeCallback,
error: errorCallback
})

WHY DO WE NEED CALLBACKS?

For one very key reason — JavaScript is an event driven language. This means that instead of waiting for a response before moving on, JavaScript will keep executing while listening for other events.

function first(){
console.log(1);
}
function second(){
console.log(2);
}
first();
second();

As the user would expect, the function first is executed first, and the function second is executed second.

// 1
// 2

THE VERDICT

You can now understand what a callback is and how it works. This is just the tip of the iceberg with callbacks, there is still a lot more to learn!

HTTP SERVER IN NODE.JS

HTTP Server in Node.js

In previous articles, we’ve introduced you to the most basic Node.js program possible. Node.js is more usually known for creating highly scalable server applications. In this article we will introduce you to a simple HTTP server built atop Node.js.

HTTP Server in Node.js

RUNNING THE SERVER

Start by creating a new file named “web_server.js”. Insert the following code into the file and save it.

var http = require(“http”);
var server = http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/html”});
response.write(“<!DOCTYPE “html”>”);
response.write(“<html>”);
response.write(“<head>”);
response.write(“<title>Hello World Page</title>”);
response.write(“</head>”);
response.write(“<body>”);
response.write(“Hello World!”);
response.write(“</body>”);
response.write(“</html>”);
response.end();
});
server.listen(80);
console.log(“Server is listening”);

To start the server, type the following command shown below. If everything works well, the user will see a message that the server is listening. Note that the instance the server attempts to bind to port 80, the standard HTTP port. If this port is already in use, or limited on user’s machine, the user will experience an error.

NODE WEB_SERVER.JS

The next step is to connect to the server using a web browser. Launch the browser of choice, and direct it to either of the following links. In networking terms, local host refers to the machine the user is currently using. The browser should be saying “Hello World!”.

http://localhost

http://127.0.0.1

HOW THE SERVER WORKS

The first thing to notice is the call to require() on line 1. Node.js provides a simple module system with a huge developer community. Node.js programs can load specific modules using the require() method. While many modules must be downloaded, some modules, such as http are included with Node.js installations.

Next, the HTTP server is created using the http module’s createServer() method. Comparable to the most Node.js functions, createServer() takes a callback function as an argument. This callback function is implemented each time the server receives a new request.

The callback function takes two arguments, request and response. The request object contains information about the client’s request. In the same way, the response object is used to return data back to the client.

The callback function begins by calling the response.writeHead() method. This method sends an HTTP status code and a collection of response headers back to the client. The status code is used to specify the result of the request.

Along with the status code, the server returns a number of HTTP headers which define the parameters of the response. If the user does not specify headers, Node.js will implicitly send them for the user.

Next, the server executes several calls to response.write(). These calls are used to write the HTML page. By default, UTF-8 character encoding is used. To be precise, all of these calls could be combined into a single call to improve performance.

After the HTML page has been written, the response.end() method is called. By calling this method, the user is telling the server that the response headers and body have been sent, and that the request has been fulfilled.

The call to listen() causes the server to bind to a port and listen for incoming connections. In order to connect to the server, clients must know exactly which port the server is listening on. Ports are identified by port numbers, with HTTP servers typically listening to port 80.

HOW TO INSTALL THE DEVELOPMENT VERSION OF DJANGO

How to install the development version of Django

Follow the below steps to Install the development version of Django Framework.

  • Check out Django’s main development branch
    $ git clone https://github.com/django/django.git
  • Make sure that the Python interpreter can load Django’s code. The most convenient way to do this is to use virtualenv, virtualenvwrapper, and pip.
  • After setting up and activating the virtualenv, run the following command:
    $ pip install -e django/

EXPLAIN THE ARCHITECTURE OF DJANGO?

Explain the Architecture of Django?

Django architecture consists of:

Models: It describes your database schema and your data structure

Views: It controls what a user sees, the view retrieves data from appropriate models and execute any calculation made to the data and pass it to the template

Templates: It determines how the user sees it. It describes how the data received from the views should be changed or formatted for display on the page

Controller: It is the heart of the system. It handles request and responses, setting up database connections and loading add-ons and specifies Django framework and URL parsing.

HOW CAN YOU SETUP DATABASE IN DJANGO?

How can you setup Database in Django?

To set up a database in Django, you can use the command edit my site/ setting.py, it is a normal python module with module level representing Django settings.

Django uses SQLite database by default. It is easy for Django users because it doesn’t require any other type of installation. In the case of other databases, you have the following keys in the DATABASE ‘default’ item to match your database connection settings.

Engines: you can change database by using ‘django.db.backends.sqlite3’ , ‘django.db.backeneds.mysql’, ‘django.db.backends.postgresql_psycopg2’, ‘django.db.backends.oracle’

Name: The name of your database. In the case if you are using SQLite as your database, in that case, a database will be a file on your computer, Name should be a full absolute path, including the file name of that file.

You can add setting likes setting like Password, Host, User, etc. in your database, if you are not choosing SQLite as your database.

WHAT ARE THE ADVANTAGES OF AEM CMS CQ5 OVER ANOTHER CMS?

What are the advantages of AEM CMS CQ5 over another CMS?

One big advantage of AEM CQ5 over another CMS (Content Management System) is how it integrates with other products from Adobe and with the Adobe Marketing Cloud. AEM comes built in with features like workflows to control content in the CMS, the use of search queries to find anything you are looking for, setting up social collaboration, tagging content, and a way to manage your digital content.

AEM also includes a way to manage mobile applications, mobile websites, e-commerce, and marketing campaign management.