Some slips for full stacks
Slip 11 :
Q.1) Develop an Express.js application that defines routes for Create operations on a resource (Movie)
// Import required libraries
const express = require('express');
const bodyParser = require('body-parser');
// Initialize the Express app
const app = express();
// Middleware to parse JSON data in request body
app.use(bodyParser.json());
// In-memory storage for movies (acting as a database)
let movies = [];
// Route to create a movie (POST request)
app.post('/movie', (req, res) => {
// Extract movie details from the request body
const { title, director, releaseYear, genre } = req.body;
// Validate required fields
if (!title || !director || !releaseYear || !genre) {
return res.status(400).json({
success: false,
message: 'All fields (title, director, releaseYear, genre) are required.'
});
}
// Create a new movie object
const newMovie = {
id: movies.length + 1, // Generate a unique ID (just for demonstration)
title,
director,
releaseYear,
genre
};
// Save the new movie to the "database"
movies.push(newMovie);
// Send success response
return res.status(201).json({
success: true,
message: 'Movie created successfully!',
data: newMovie
});
});
// Route to get all movies (GET request for testing)
app.get('/movies', (req, res) => {
return res.status(200).json({
success: true,
data: movies
});
});
// Error handler for undefined routes
app.use((req, res) => {
return res.status(404).json({
success: false,
message: 'Route not found'
});
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Q.2) Create Angular application that print the name of students who play basketball using filter and map method.
import { Component } from '@angular/core';
@Component({
selector: 'app-student-list',
templateUrl: './student-list.component.html',
styleUrls: ['./student-list.component.css']
})
export class StudentListComponent {
// Array of students with their names and the sports they play
students = [
{ name: 'Alice', sports: ['Basketball', 'Football'] },
{ name: 'Bob', sports: ['Football', 'Cricket'] },
{ name: 'Charlie', sports: ['Basketball', 'Tennis'] },
{ name: 'David', sports: ['Cricket'] },
{ name: 'Eva', sports: ['Basketball', 'Hockey'] }
];
// Method to get the names of students who play Basketball
getBasketballPlayers() {
return this.students
.filter(student => student.sports.includes('Basketball')) // Filter students who play basketball
.map(student => student.name); // Map the result to just the student names
}
}
Slip 4 :
Q.1) Fetch the details using ng-repeat in AngularJS [15]
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AngularJS ng-repeat Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
<div ng-controller="BankController">
<h2>Bank Details</h2>
<!-- Table to display bank details using ng-repeat -->
<table border="1">
<thead>
<tr>
<th>Bank Name</th>
<th>MICR Code</th>
<th>IFSC Code</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- Using ng-repeat to iterate over the 'banks' array -->
<tr ng-repeat="bank in banks">
<td>{{bank.name}}</td>
<td>{{bank.micrCode}}</td>
<td>{{bank.ifscCode}}</td>
<td>{{bank.address}}</td>
</tr>
</tbody>
</table>
</div>
<script>
// Define AngularJS module and controller
var app = angular.module('myApp', []);
app.controller('BankController', function($scope) {
// Data for banks
$scope.banks = [
{ name: 'ABC Bank', micrCode: '123456789', ifscCode: 'ABC123', address: '123 Main St, City A' },
{ name: 'XYZ Bank', micrCode: '987654321', ifscCode: 'XYZ987', address: '456 Second St, City B' },
{ name: 'LMN Bank', micrCode: '112233445', ifscCode: 'LMN456', address: '789 Third St, City C' },
{ name: 'PQR Bank', micrCode: '998877665', ifscCode: 'PQR321', address: '101 First St, City D' }
];
});
</script>
</body>
</html>
Q.2) Express.js application to include middleware for parsing request bodies (e.g., JSON, form data) and validating input data.
const express = require('express');
const { body, validationResult } = require('express-validator');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse JSON and form data
app.use(bodyParser.json()); // For JSON payloads
app.use(bodyParser.urlencoded({ extended: true })); // For form data (x-www-form-urlencoded)
// Route with input validation
app.post('/submit', [
// Validate and sanitize input data using express-validator
body('name').isString().withMessage('Name must be a string').notEmpty().withMessage('Name is required'),
body('email').isEmail().withMessage('Valid email is required').normalizeEmail(),
body('age').isInt({ min: 18 }).withMessage('Age must be a number and at least 18'),
], (req, res) => {
// Check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
// If validation is successful, process the request data
const { name, email, age } = req.body;
res.status(200).json({
message: 'Data received successfully',
data: {
name,
email,
age
}
});
});
// Start the server
app.listen(port, () => {
console.log(`Server is ru
nning at http://localhost:${port}`);
});
Thankssss
ReplyDelete