Posts

Showing posts from October, 2024

FSD-II Practical 3

Image
  3.Create a custom life-cycle hook for logging component life-cycle events  - Define a custom life-cycle hook named `LogLifecycle` that logs component initialization, destruction, and changes in input properties. - Implement the custom hook in a sample component.  - Test the behavior by adding the component to a parent component and observing the logs.   1.       ng new practical3 2.       cd practical3 3.       ng generate service log-lifecycle 4.        open the file            log-lifecycle.service.ts         import { Injectable , OnInit , OnDestroy , OnChanges , SimpleChanges } from '@angular/core' ;   @ Injectable ({   providedIn: 'root' }) export class LogLifecycle implements OnInit , OnDestroy , OnChanges {     constructor () {     co...

FSD-II Practical Assignment-10

  10.Build a small application that uses multiple modules to perform specific tasks (e.g., file manipulation, data processing). const fs = require('fs').promises; // Using promises for better async handling  async function main()  {  try  { // **Read from a file**  const data = await fs.readFile('input.txt', 'utf8');  console.log('File content:', data);  // **Write to a new file**  const outputData = { message: "This is written to output1.json"};  await fs.writeFile('output1.json', JSON.stringify(outputData, null, 2));  console.log('Data written to output.json'); // **Append data to an existing file**  const additionalContent = "\nThis line is appended to input.txt.";  await fs.appendFile('input.txt', additionalContent);  console.log('Data appended to input.txt');  // **Delete a file** await fs.unlink('output1.json');  console.log('output1.json deleted successfully');  } catch (error) { ...

FSD-II Practical Assignment-13(Slip13_expressJS)

 13.Extend the previous Express.js application to include middleware for parsing request bodies (e.g., JSON, form data) and validating input data. Send appropriate JSON responses for success and error cases. const express = require ( 'express' ); const { body , validationResult } = require ( 'express-validator' ); const app = express (); const port = 3000 ; // Middleware to parse JSON bodies app . use ( express . json ()); // Middleware to parse URL-encoded bodies app . use ( express . urlencoded ({ extended : true })); // Example route with input validation app . post ( '/submit' , [ body ( 'username' ) . isString () . trim () . notEmpty () . withMessage ( 'Username is required' ), body ( 'email' ) . isEmail () . withMessage ( 'Must be a valid email address' ), ], ( req , res ) => { // Check for validation errors const errors = validationResult (...