unit 1
unit 1
Defining the endpoints of an application and the way the application responds to the incoming client
requests are referred to as routing. A route is nothing but a mixture of a URI, HTTP request method,
and some handlers for that particular path.
Application Instance
Application Instance (Method 1): Routes are defined directly on the app object using methods like
app.get(), app.post(), etc. It's simple and straightforward but can become less manageable in large
applications.
Router Class (Method 2): Routes are grouped together in a modular Router instance. This makes it
easier to organize and maintain routes for different sections of the application. It also allows you to
reuse route modules across different parts of your application.
1. b) Explain about handling routes.
Handling Routes: Route handler can be defined as functions that get executed every time the
server receives a request for a particular URL path and HTTP method.
Request object The HTTP request object is created when a client makes a request to the
server. The variable named req is used to represent this object.
The request object in Express provides many properties and methods to access the
request related information sent by the client to the server.
Response object
The HTTP response object has information about the response sent from the server to
the client.
The response object is created along with the request object and is commonly
represented by a variable named res.
The route handler function sends a response using res.send() method whenever a GET
request is received.
res.send('About us page');
The response object in Express also supports many methods for sending different types of
responses.
commonly used methods of the response object:
o Mongoose automatically type-casts data according to the schema. If data does not
match the defined schema types, Mongoose handles the casting or rejects the data,
ensuring consistency.
3. Data Validation:
4. Middleware Support:
o Mongoose allows the use of pre and post hooks (middleware) to run functions
before or after certain actions (e.g., saving, updating). This helps to integrate
additional logic in the application lifecycle.
1. Schema:
o The schema defines the structure of the document. It is essentially a blueprint that
describes the fields in the document, their types, and validation rules.
o Example:
empId: Number,
empName: String,
address: {
doorNo: String,
lane: String,
pincode: Number
});
2. Model:
o Example:
o Mongoose supports a variety of data types (e.g., String, Number) and validation
rules (e.g., required, min, max).
name: {
type: String,
required: true
},
age: {
type: Number,
min: 18
}};
4. Default Values:
o You can set default values for fields in the schema, which will be applied when the
field is not provided during document creation.
o Example:
});
5. Custom Validators:
o Mongoose allows you to define custom validation rules for fields if built-in validators
are insufficient.
o Example:
email: {
type: String,
validate: {
validator: function(v) {
return /.+@.+\..+/.test(v);
},
});
10. What are sessions. Explain about session management techniques cookies and session store
A session is a way to store information (in variables) to be used across multiple pages
(requests) of a web application.
Sessions typically store user login details, cart items, or any data that should persist during a
visit.
Session Management is a technique used by the webserver to store a particular user's session
information. The Express framework provides a consistent interface to work with the session-related
data. The framework provides two ways of implementing sessions: By using cookies By using the
session store
(i) Cookies:
It is sent back and forth with every request/response between client and server.
Types:
Advantages:
Lightweight
Easy to implement
Limitations:
The client gets a session ID stored in a cookie, and the server maps that ID to the session
data.
A session store can be used to store the session data in the back-end.
Using this approach, a large amount of data can be stored, and also the data will be hidden
from the user, unlike a cookie.
File-based
app.use(session({
secret: 'yourSecret',
resave: false,
saveUninitialized: true
}));
Advantages: