WEBX Solutions
WEBX Solutions
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.
• 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.
app.listen(3000);
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
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:
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>
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:
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:
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:
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 })
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:
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" });