WC QB 2
WC QB 2
WC QB 2
REPL Commands
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 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.
Advantages of 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.
EX: -
res.send('Hello World!')
})
app.listen(port, () => {
})
OUTPUT: -
Hello World!
Q6. Explain Rest API with example.
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.
MVC Components
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
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.
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:
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
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",
Import useState
To use the useState Hook, we first need to import it into our component.
1. Dispatcher
2. Stores
3. Views (React components)
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.