Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

WC QB 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

WC QB

Q1. Difference between symmetric and asymmetric encryption algorithms.


Q2. REPL.
REPL stands for Read-Eval-Print Loop and it represents a computer
environment like a Windows console or Unix/Linux shell where a
command is entered and the system responds with output in an
interactive mode. Node.js or Node comes bundled with a REPL
environment. It performs the following tasks −
• Read − Reads user's input, parses the input into JavaScript
data structure, and stores in memory.
• Eval − Takes and evaluates the data structure.
• Print − Prints the result.
• Loop − Loops the above command until the user presses ctrl-
c twice.
The REPL feature of Node is very useful in experimenting with Node.js
codes and to debug JavaScript codes.

REPL Commands

• ctrl + c − terminate the current command.


• ctrl + c twice − terminate the Node REPL.
• ctrl + d − terminate the Node REPL.
• Up/Down Keys − see command history and modify previous
commands.
• tab Keys − list of current commands.
• .help − list of all commands.
• .break − exit from multiline expression.
• .clear − exit from multiline expression.
• .save filename − save the current Node REPL session to a file.
• .load filename − load file content in current Node REPL
session.
Starting REPL
REPL can be started by simply running node on shell/console without
any arguments as follows.
$ node
You will see the REPL Command prompt > where you can type any
Node.js command −
$ node
>
Simple Expression
Let's try a simple mathematics at the Node.js REPL command prompt −
$ node
>1+3
4
>1+(2*3)-4
3
>

Stopping REPL

As mentioned above, you will need to use ctrl-c twice to come out of
Node.js REPL.
$ node
>
(^C again to quit)
>
Q3. Web server architecture in detail.
A web server is a computer where the web content is stored. Basically,
the web server is used to host websites but there exist other web
servers also such as gaming, storage, FTP, email etc.

Web Server Working

Web server respond to the client request in either of the following two
ways:
• Sending the file to the client associated with the requested
URL.
• Generating response by invoking a script and communicating
with database

• When the client sends request for a web page, the web server
search for the requested page if requested page is found then it
will send it to client with an HTTP response.
• If the requested web page is not found, web server will the send
an HTTP response:Error 404 Not found.
• If client has requested for some other resources then the web
server will contact to the application server and data store to
construct the HTTP response.
• Architecture
Web Server Architecture follows the following two approaches:
1. Concurrent Approach
2. Single-Process-Event-Driven Approach.
Concurrent Approach
Concurrent approach allows the web server to handle multiple client
requests at the same time. It can be achieved by following methods:
•Multi-process
• Multi-threaded
• Hybrid method.
Multi-processing
In this a single process (parent process) initiates several single-threaded
child processes and distribute incoming requests to these child
processes. Each of the child processes are responsible for handling single
request.
It is the responsibility of parent process to monitor the load and decide if
processes should be killed or forked.
Multi-threaded
Unlike Multi-process, it creates multiple single-threaded process.
Hybrid
It is combination of above two approaches. In this approach multiple
process are created and each process initiates multiple threads. Each of
the threads handles one connection. Using multiple threads in single
process results in less load on system resources.
Q4. Event loop in Nodejs.
Node.js is a single-threaded event-driven platform that is capable of
running non-blocking, asynchronously programming. These
functionalities of Node.js make it memory efficient. The event loop
allows Node.js to perform non-blocking I/O operations despite the fact
that JavaScript is single-threaded. It is done by assigning operations to
the operating system whenever and wherever possible.

Most operating systems are multi-threaded and hence can handle


multiple operations executing in the background. When one of these
operations is completed, the kernel tells Node.js and the respective
callback assigned to that operation is added to the event queue which
will eventually be executed. This will be explained further in detail later
in this topic.
Features of Event Loop:
• Event loop is an endless loop, which waits for tasks, executes
them and then sleeps until it receives more tasks.
• The event loop executes tasks from the event queue only when
the call stack is empty i.e. there is no ongoing task.
• The event loop allows us to use callbacks and promises.
• The event loop executes the tasks starting from the oldest first.
Example:
console.log("This is the first statement");
setTimeout(function(){
console.log("This is the second statement");
}, 1000);
console.log("This is the third statement");
Output:
This is the first statement
This is the third statement
Working of the Event loop: When Node.js starts, it initializes the event loop,
processes the provided input script which may make async API calls, schedule
timers, then begins processing the event loop. In the previous example, the
initial input script consisted of console.log() statements and a setTimeout()
function which schedules a timer.
When using Node.js, a special library module called libuv is used to perform
async operations. This library is also used, together with the back logic of
Node, to manage a special thread pool called the libuv thread pool.This thread
pool is composed of four threads used to delegate operations that are too
heavy for the event loop. I/O operations, Opening and closing connections,
setTimeouts are the example of such operations.
Q5. Explain expreasss.js with advanteges and example.

Express is a fast, assertive, essential and moderate web framework of


Node.js. You can assume express as a layer built on the top of the
Node.js that helps manage a server and routes. It provides a robust set
of features to develop web and mobile applications.

the core features of Express framework:


o It can be used to design single-page, multi-page and hybrid web
applications.
o It allows to setup middlewares to respond to HTTP Requests.
o It defines a routing table which is used to perform different actions
based on HTTP method and URL.
o It allows to dynamically render HTML Pages based on passing
arguments to templates.

Advantages of Express.js

1. Makes Node.js web application development fast and easy.


2. Easy to configure and customize.
3. Allows you to define routes of your application based on HTTP
methods and URLs.
4. Includes various middleware modules which you can use to perform
additional tasks on request and response.
5. Easy to integrate with different template engines like Jade, Vash, EJS
etc.
6. Allows you to define an error handling middleware.
7. Easy to serve static files and resources of your application.
8. Allows you to create REST API server.
9. Easy to connect with databases such as MongoDB, Redis, MySQL
Install Express.js

You can install express.js using npm. The following command will
install latest version of express.js globally on your machine so that
every Node.js application on your machine can use it.

npm install -g express

The following command will install latest version of express.js local to


your project folder.

C:\MyNodeJSApp> npm install express --save

--save will update the package.json file by specifying express.js


dependency.

EX: -

const express = require('express')

const app = express()

const port = 3000

app.get('/', (req, res) => {

res.send('Hello World!')

})

app.listen(port, () => {

console.log(`Example app listening on port ${port}`)

})

OUTPUT: -

Hello World!
Q6. Explain Rest API with example.

Representational State Transfer (REST) is an architectural style that


defines a set of constraints to be used for creating web services. REST
API is a way of accessing web services in a simple and flexible way
without having any processing.

REST technology is generally preferred to the more robust Simple Object


Access Protocol (SOAP) technology because REST uses less bandwidth,
simple and flexible making it more suitable for internet usage. It’s used
to fetch or give some information from a web service. All communication
done via REST API uses only HTTP request.

Working: A request is sent from client to server in the form of a web


URL as HTTP GET or POST or PUT or DELETE request. After that, a
response comes back from the server in the form of a resource which
can be anything like HTML, XML, Image, or JSON. But now JSON is the
most popular format being used in Web Services.

In HTTP there are five methods that are commonly used in a REST-based
Architecture i.e., POST, GET, PUT, PATCH, and DELETE. These correspond
to create, read, update, and delete (or CRUD) operations respectively.
There are other methods which are less frequently used like OPTIONS
and HEAD.
• GET: The HTTP GET method is used to read (or retrieve) a
representation of a resource. In the safe path, GET returns a
representation in XML or JSON and an HTTP response code of 200
(OK). In an error case, it most often returns a 404 (NOT FOUND) or
400 (BAD REQUEST).
• POST: The POST verb is most often utilized to create new
resources. In particular, it’s used to create subordinate resources.
That is, subordinate to some other (e.g. parent) resource. On
successful creation, return HTTP status 201, returning a Location
header with a link to the newly-created resource with the 201 HTTP
status.

• PUT: It is used for updating the capabilities. However, PUT can also
be used to create a resource in the case where the resource ID is
chosen by the client instead of by the server. In other words, if the
PUT is to a URI that contains the value of a non-existent resource
ID. On successful update, return 200 (or 204 if not returning any
content in the body) from a PUT. If using PUT for create, return
HTTP status 201 on successful creation. PUT is not safe operation
but it’s idempotent.

• PATCH: It is used to modify capabilities. The PATCH request only


needs to contain the changes to the resource, not the complete
resource. This resembles PUT, but the body contains a set of
instructions describing how a resource currently residing on the
server should be modified to produce a new version. This means
that the PATCH body should not just be a modified part of the
resource, but in some kind of patch language like JSON Patch or
XML Patch. PATCH is neither safe nor idempotent.

• DELETE: It is used to delete a resource identified by a URI. On


successful deletion, return HTTP status 200 (OK) along with a
response body.
Q8. what is MVC with example.
The Model-View-Controller (MVC) is an architectural pattern that
separates an application into three main logical components:
the model, the view, and the controller. Each of these components are
built to handle specific development aspects of an application. MVC is
one of the most frequently used industry-standard web development
framework to create scalable and extensible projects.

MVC Components

Following are the components of MVC −

Model
The Model component corresponds to all the data-related logic that the
user works with. This can represent either the data that is being
transferred between the View and Controller components or any other
business logic-related data. For example, a Customer object will retrieve
the customer information from the database, manipulate it and update it
data back to the database or use it to render data.
View
The View component is used for all the UI logic of the application. For
example, the Customer view will include all the UI components such as
text boxes, dropdowns, etc. that the final user interacts with.
Controller
Controllers act as an interface between Model and View components to
process all the business logic and incoming requests, manipulate data
using the Model component and interact with the Views to render the
final output. For example, the Customer controller will handle all the
interactions and inputs from the Customer View and update the database
using the Customer Model. The same controller will be used to view the
Customer data.

ASP.NET MVC

ASP.NET supports three major development models: Web Pages, Web


Forms and MVC (Model View Controller). ASP.NET MVC framework is a
lightweight, highly testable presentation framework that is integrated
with the existing ASP.NET features, such as master pages,
authentication, etc. Within .NET, this framework is defined in the
System.Web.Mvc assembly. The latest version of the MVC Framework is
5.0. We use Visual Studio to create ASP.NET MVC applications which
can be added as a template in Visual Studio.
ASP.NET MVC Features
ASP.NET MVC provides the following features −
• Ideal for developing complex but lightweight applications.
• Provides an extensible and pluggable framework, which can
be easily replaced and customized. For example, if you do not
wish to use the in-built Razor or ASPX View Engine, then you
can use any other third-party view engines or even customize
the existing ones.
• Utilizes the component-based design of the application by
logically dividing it into Model, View, and Controller
components. This enables the developers to manage the
complexity of large-scale projects and work on individual
components.
• MVC structure enhances the test-driven development and
testability of the application, since all the components can be
designed interface-based and tested using mock objects.
Hence, ASP.NET MVC Framework is ideal for projects with
large team of web developers.
• Supports all the existing vast ASP.NET functionalities, such
as Authorization and Authentication, Master Pages, Data
Binding, User Controls, Memberships, ASP.NET Routing, etc.
• Does not use the concept of View State (which is present in
ASP.NET). This helps in building applications, which are
lightweight and gives full control to the developers.

Q7. Express generator, Authentication in Express.js.


In this tutorial, we’ll be discussing the concepts of the Node.js Express
generator tool. The Express-generator package is a utility that provides a
command-line tool you can use to scaffold your project - ie create
boilerplate folder structure, files and code.

It’s a package that you can use to simplify your development time while
developing your Node.js web applications. As the name suggests, an
Express generator is a tool that we’ll be using to generate our
application, that otherwise, we would’ve created manually.
Q9. React Hook ,
React Hooks

Hooks are the new feature introduced in the React 16.8 version. It allows
you to use state and other React features without writing a class. Hooks
are the functions which "hook into" React state and lifecycle features from
function components. It does not work inside classes.

Hooks are backward-compatible, which means it does not contain any


breaking changes. Also, it does not replace your knowledge of React
concepts.

When to use a Hooks

If you write a function component, and then you want to add some state
to it, previously you do this by converting it to a class. But, now you can
do it by using a Hook inside the existing function component.

Rules of Hooks

Hooks are similar to JavaScript functions, but you need to follow these two
rules when using them. Hooks rule ensures that all the stateful logic in a
component is visible in its source code. These rules are:

1. Only call Hooks at the top level

Do not call Hooks inside loops, conditions, or nested functions. Hooks


should always be used at the top level of the React functions. This rule
ensures that Hooks are called in the same order each time a components
renders.

2. Only call Hooks from React functions

You cannot call Hooks from regular JavaScript functions. Instead, you can
call Hooks from React function components. Hooks can also be called
from custom Hooks.
React Hooks Installation

To use React Hooks, we need to run the following commands:

1. $ npm install react@16.8.0-alpha.1 --save


2. $ npm install react-dom@16.8.0-alpha.1 --save

The above command will install the latest React and React-DOM alpha
versions which support React Hooks. Make sure the package.json file lists
the React and React-DOM dependencies as given below.

1. "react": "^16.8.0-alpha.1",
2. "react-dom": "^16.8.0-alpha.1",

The React useState Hook allows us to track state in a function


component. State generally refers to data or properties that need to be
tracking in an application.

Import useState

To use the useState Hook, we first need to import it into our component.

import { useState } from "react";


Q10. Flow OF React Flux.

React Flux Concept

Flux is an application architecture that Facebook uses internally for


building the client-side web application with React. It is not a library nor a
framework. It is neither a library nor a framework. It is a kind of architecture
that complements React as view and follows the concept of Unidirectional
Data Flow model. It is useful when the project has dynamic data, and we
need to keep the data updated in an effective manner. It reduces the
runtime errors.

Flux applications have three major roles in dealing with data:

1. Dispatcher
2. Stores
3. Views (React components)

Here, you should not be confused with the Model-View-Controller (MVC)


model. Although, Controllers exists in both, but Flux controller-views
(views) found at the top of the hierarchy. It retrieves data from the stores
and then passes this data down to their children. Additionally, action
creators - dispatcher helper methods used to describe all changes that are
possible in the application. It can be useful as a fourth part of the Flux
update cycle.

Structure and Data Flow


In Flux application, data flows in a single direction(unidirectional). This
data flow is central to the flux pattern. The dispatcher, stores, and views
are independent nodes with inputs and outputs. The actions are simple
objects that contain new data and type property. Now, let us look at the
various components of flux architecture one by one.

Dispatcher :- It is a central hub for the React Flux application and


manages all data flow of your Flux application. It is a registry of callbacks
into the stores. It has no real intelligence of its own, and simply acts as a
mechanism for distributing the actions to the stores.
Stores :- It primarily contains the application state and logic. It is similar
to the model in a traditional MVC. It is used for maintaining a particular
state within the application, updates themselves in response to an action,
and emit the change event to alert the controller view.
Views :- It is also called as controller-views. It is located at the top of the
chain to store the logic to generate actions and receive new data from the
store. It is a React component listen to change events and receives the
data from the stores and re-render the application.
Actions :- The dispatcher method allows us to trigger a dispatch to the
store and include a payload of data, which we call an action. It is an action
creator or helper methods that pass the data to the dispatcher.

Advantage of Flux
o It is a unidirectional data flow model which is easy to understand.
o It is open source and more of a design pattern than a formal
framework like MVC architecture.
o The flux application is easier to maintain.
o The flux application parts are decouple
o
Q11. difference between MVC and flux and node.JS and Express.JS.

MVC and flux


node.JS and Express.JS

You might also like