0% found this document useful (0 votes)
24 views

WEBX Solutions

Uploaded by

Om Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

WEBX Solutions

Uploaded by

Om Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Module 4

1. State cookies in express


• Cookies are small pieces of data that are stored on the client side (browser) in the form of a key-
value pair.
• Cookies are used for session management, user preference,a and tracking of user behavior.
• When user loads the website a cookie is sent with the request that helps us to track the user’s actions.
• To use cookies in Express, you have to install the cookie-parser package.
• It is a middleware that is used to parse cookies from the incoming request.

npm install cookie-parser

2. Differentiate Authentication and Authorization.

Feature Authentication Authorization


Process of determining what resources a user can
Definition Process of verifying the identity of a user or system. access and what actions they can perform.
Determines whether an authenticated user has
Purpose Determines whether someone is who they claim to be. permission to access specific resources.
Verifies the identity through credentials such as Verifies the permissions associated with an
Verification username/password, biometrics, tokens, etc. authenticated identity.
Logging into an email account by providing username and A user with admin privileges accessing administrative
Example password. features of a system.
Focus Establishes the identity of the user. Focuses on what actions the user is allowed to perform.

3. Explain working of session in Express.

In Express, sessions are managed using middleware like express-session. Here's a brief overview of how they
work:

1. Initialization: You include the express-session middleware in your Express application and configure
it with options like a secret key and session store.
2. Session Creation: When a user makes a request, Express generates a unique session ID and sends it to
the client as a cookie. This ID is used to associate subsequent requests with the same session.
3. Data Storage: You can store user-specific data in the session object (req.session). This data is
serialized and stored on the server-side, typically in a session store like a database or in-memory
storage.
4. Data Access: Throughout the user's session, you can access and modify session data on the req.session
object. This allows you to maintain user state across multiple requests.
5. Authentication and Authorization: Sessions are commonly used for user authentication and
authorization. You can store user IDs, roles, or permissions in the session to determine access levels
and enforce security rules.
6. Expiration and Termination: Sessions typically have an expiration time to prevent them from
persisting indefinitely. You can configure the session expiration time based on your application's
requirements. Sessions should be destroyed when a user logs out or their session expires to remove any
stored data and invalidate the session ID.

4. Enlist features of Express.

• Minimalist Framework: Express is a minimalist web framework for Node.js, providing a thin
layer of features on top of Node's HTTP module.
• Middleware Support: Express has robust middleware support, allowing you to easily add
functionality to your application's request/response cycle.
• Routing: Express provides a simple and flexible routing mechanism that allows you to define
routes for handling different HTTP requests.
• Error Handling: Express provides built-in error handling middleware and allows you to define
custom error handling logic.
• RESTful APIs: Express is commonly used for building RESTful APIs due to its simplicity,
flexibility, and performance.
• Scalability: Express is highly scalable and can be used to build both small-scale and large-scale
applications, from simple websites to complex web applications.

5. Write a program to create a server using Express displaying “Hello World” message.

var express = require('express');


var app = express();

app.get('/', function(req, res){


res.send("Hello world!");
});

app.listen(3000);

Save the file, go to your terminal and type the following.

nodemon index.js

This will start the server. To test this app, open your browser and go to http://localhost:3000 and a
message will be displayed as in the following screenshot.
Module 5

1. Demonstrate Controller in AngularJS with Example. (REMAINING)

2. Discuss Angular Js express.

AngularJS and Express are two separate technologies that are commonly used together to build web
applications. Here's a brief overview of each:

1. AngularJS:
• AngularJS is a front-end JavaScript framework developed by Google for building dynamic
web applications.
• It provides features like two-way data binding, dependency injection, directives, and routing,
which make it easy to create interactive and single-page applications.
2. Express:
• Express.js is a back-end web application framework for Node.js, designed for building web
servers and APIs.
• It provides a simple and minimalistic approach to building web applications, with features like
routing, middleware support, and template rendering.

Here's a simple workflow of how AngularJS and Express interact in a typical web application:

1. The AngularJS client sends an HTTP request to an Express route.


2. Express receives the request, processes it, and interacts with a database or other services as needed.
3. Express sends back a response containing data or other information requested by the AngularJS client.
4. AngularJS receives the response and updates the UI accordingly, displaying data to the user or
performing other actions as necessary.

3. Illustrate the use of AngularJs ng-app, ng-init,ng-model in detail.

ng-app:
• The ng-app directive initializes an AngularJS application within the specified HTML element. It
marks the root element of the AngularJS application.
• Example:
<div ng-app=“myApp”>
….
</div>

ng-init:
• The ng-init directive initializes variables or expressions in the AngularJS scope.
• Example:
<div ng-app=“myApp” ng-init=”firstName=’Indu’; lastname=’Gupta’”>
….
</div>

ng-model:
• The ng-model directive binds the value of HTML controls (like <input>, <select>, <textarea>) to
application data.
• It creates a two-way data binding between the view and the model.
• Example:
<div ng-app=“myApp”>
….
<input type=”text” ng-model=”myname”>
<p> My name is (enter it): {{myname}} </p>
</div>

4. Illustrate Angular JS Custom directives with example. (REMAINING)

5. Explain Routing using ngroute,ng-repeat,ng-view,ng-style with suitable example.


Module 6

1. Demonstrate Accessing and Manipulating Databases commands in MongoDB.

Here's a demonstration of accessing and manipulating databases using MongoDB commands:

1. Accessing MongoDB: To access MongoDB, you typically start by launching the MongoDB shell. You
can do this by running the mongo command in your terminal or command prompt.
2. Connecting to a Database: Once you're in the MongoDB shell, you can connect to a specific database
using the use command. For example, to connect to a database named mydatabase, you would type:
use mydatabase
3. Inserting Data: You can insert data into a collection using the insertOne() or insertMany() methods.
For example, to insert a document into a collection named users, you would use the insertOne()
method:

db.users.insertOne({ name: "John", age: 30 })

4. Querying Data: You can query data from a collection using the find() method. For example, to
retrieve all documents from the users collection, you would use:

db.users.find()

5. Updating Data: You can update documents in a collection using the updateOne() or updateMany()
methods. For example, to update the age of a user named "John" in the users collection, you would use
the updateOne() method:

db.users.updateOne({ name: "John" }, { $set: { age: 35 } })

6. Deleting Data: You can delete documents from a collection using the deleteOne() or deleteMany()
methods. For example, to delete a user named "John" from the users collection, you would use the
deleteOne() method:

db.users.deleteOne({ name: "John" })

7. Indexing: You can create indexes on fields in a collection to improve query performance. For example,
to create an index on the name field in the users collection, you would use the createIndex() method:

db.users.createIndex({ name: 1 })

2. Discuss advantages of Mongo DB

MongoDB offers many advantages over traditional relational databases:

• Full cloud-based developer data platform


• Flexible document schemas
• Widely supported and code-native data access
• Change-friendly design
• Powerful querying and analytics
• Easy horizontal scale-out with sharding
• Simple installation
• Cost-effective
• Full technical support and documentation
3. Explain MongoDB Data Types along with syntax.

MongoDB supports a variety of data types to represent data in documents stored within collections. Here's an
overview of the common data types along with their syntax:

1. String: Represents UTF-8 encoded strings. Syntax: "key" : "value"


2. Integer: Represents a 32-bit signed integer value. Syntax: "key" : <integer_value>
3. Double: Represents a 64-bit floating-point value. Syntax: "key" : <double_value>
4. Boolean: Represents a boolean value (true or false). Syntax: "key" : true or "key" : false
5. Date: Represents a date and time. Syntax: "key" : ISODate("YYYY-MM-DDTHH:MM:SSZ")
6. Array: Represents an ordered collection of values. Syntax: "key" : [value1, value2, ...]
7. Object: Represents an embedded document. Syntax: "key" : {"subkey1" : value1, "subkey2" :
value2, ...}
8. Null: Represents a null value. Syntax: "key" : null
9. ObjectId: Represents a 12-byte identifier used as a unique identifier for documents. Syntax: "key" :
ObjectId("<hexadecimal_value>")
10. Binary Data: Represents binary data. Syntax: "key" : BinData(<subtype>,
"<base64_encoded_data>")

4. Explain Mongo DB CRUD operation with Example.

MongoDB CRUD operations refer to Create, Read, Update, and Delete operations that can be performed on
documents within collections. Here's an explanation of each operation along with an example:

1. Create (Insert):
• Inserting new documents into a collection.
• Syntax: db.collection.insertOne(document) for inserting a single document,
db.collection.insertMany(documents) for inserting multiple documents.
• Example:
// Inserting a single document into a collection
db.users.insertOne({
name: "John Doe",
age: 30,
email: "[email protected]"
});

2. Read (Query):
• Retrieving documents from a collection.
• Syntax: db.collection.find(query, projection) where query specifies the selection criteria and
projection specifies which fields to include or exclude.
• Example:
// Finding documents that match a query
db.users.find({ age: { $gte: 25 } });

3. Update:
• Modifying existing documents within a collection.
• Syntax: db.collection.updateOne(filter, update, options) for updating a single document,
db.collection.updateMany(filter, update, options) for updating multiple documents.
• Example:
// Updating a document that matches a query
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 35 } }
);
Delete:
• Removing documents from a collection.
• Syntax: db.collection.deleteOne(filter) for deleting a single document,
db.collection.deleteMany(filter) for deleting multiple documents.
• Example:
/ Deleting a document that matches a query
db.users.deleteOne({ name: "John Doe" });

5. Explain in detail different rules for REST API

1. Use Descriptive URIs:


• URIs should reflect the resources they point to and should be descriptive.
2. Use Nouns for Resources:
• Resources should be named using nouns rather than verbs.
3. Use HTTP Methods Appropriately:
• Use HTTP methods (GET, POST, PUT, DELETE) for their intended purpose.
4. Use HTTP Status Codes:
• Use appropriate HTTP status codes to indicate the status of the request.
5. Use Plural Nouns for Collections:
• Use plural nouns to represent collections of resources.
6. Versioning:
• Include API versioning in the URI to ensure backward compatibility when API changes are
made.
7. Use Query Parameters for Filtering, Sorting, Pagination:
• Use query parameters for filtering, sorting, and pagination to make API endpoints more
flexible.
8. Use HTTP Headers for Metadata:
• Use HTTP headers for metadata such as authentication tokens, content type, caching, etc.
9. Statelessness:
• Each request to the API should contain all the information necessary for the server to fulfill
the request. The server should not rely on previous requests or maintain session state.
10. Use JSON for Data Representation:
• JSON is the most commonly used format for data representation in RESTful APIs due to its
simplicity and readability.
11. Handle Errors Gracefully:
• Provide meaningful error messages and appropriate HTTP status codes when an error occurs.
12. Security:
• Implement appropriate security measures such as authentication (e.g., OAuth, JWT) and
authorization to protect the API from unauthorized access.

You might also like