0% found this document useful (0 votes)
6 views5 pages

CS4201_Assignment (1)

assignment
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)
6 views5 pages

CS4201_Assignment (1)

assignment
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/ 5

Trijal P G

Cyber Security

CS4201 WEB DEVELOPMENT FRAMEWORKS AND PRACTICES​


ASSIGNMENT - 1

A. Node.js Programs

1. Display a Message in the Browser

const ​
http = require('http');​
const server = http.createServer((req, res) => {​
res.writeHead(200, { 'Content-Type': 'text/html' });​
res.write('<h1>Welcome to my Node.js Server!</h1>');​
res.end();​
});​

server.listen(3000, () => {​
console.log('Server running at http://localhost:3000');​
});

2. Display the Current Date and Time in the Browser​

const http = require('http');​


const server = http.createServer((req, res) => {​
res.writeHead(200, { 'Content-Type': 'text/html' });​
res.write(`<h1>Current Date and Time: ${new Date()}</h1>`);​
res.end();​
});​
server.listen(3000, () => {​
console.log('Server running at http://localhost:3000');​
});

3. Pass a Query String in URL​

const http = require('http');​


const url = require('url');​
const server = http.createServer((req, res) => {​
res.writeHead(200, { 'Content-Type': 'text/html' });​
const queryObject = url.parse(req.url, true).query;​


res.write(`<h1>Query Parameters:
${JSON.stringify(queryObject)}</h1>`);​
res.end();​
});​
server.listen(3000, () => {​
console.log('Server running at http://localhost:3000');​
});

4. Splitting Up a Web Address into Readable Parts​

const url = require('url');​


const address = 'https://example.com/path?name=John';​
const parsedUrl = url.parse(address, true);​
console.log(parsedUrl);​
5. Display Customized Error Message​
const http = require('http');​
const server = http.createServer((req, res) => {​
res.writeHead(404, { 'Content-Type': 'text/html' });​
res.write('<h1>404 - Page Not Found</h1>');​
res.end();​
});​
server.listen(3000, () => {​
console.log('Server running at http://localhost:3000');​
});​


6. Event Handling​

const events = require('events');​


const eventEmitter = new events.EventEmitter();​
eventEmitter.on('customEvent', () => {​
console.log('Custom event triggered!');​
});​
eventEmitter.emit('customEvent');

B. Express.js Programs

a. Implementation of Two Middlewares

const express = require('express');​


const app = express();​
const middleware1 = (req, res, next) => {​
console.log('Middleware 1 executed');​
next();​
};​
const middleware2 = (req, res, next) => {​
console.log('Middleware 2 executed');​
next();​
};​
app.use(middleware1);​
app.use(middleware2);


b. Filtering Paths Using URL Prefix

app.get('/api/*', (req, res) => {​


res.send('API Route Accessed!');​
});​


c. Setting the Status Code

app.get('/status', (req, res) => {​


res.status(201).send('Resource Created Successfully');​
});


d. Parsing Data from Request​

const bodyParser = require('body-parser');​


app.use(bodyParser.json());​
app.post('/data', (req, res) => {​
res.send(`Received Data: ${JSON.stringify(req.body)}`);​
});


e. Demonstrating GET, POST, PUT, and DELETE Requests​

app.get('/user', (req, res) => res.send('GET Request for User'));​


app.post('/user', (req, res) => res.send('POST Request for User'));​


app.put('/user', (req, res) => res.send('PUT Request for User'));​
app.delete('/user', (req, res) => res.send('DELETE Request for
User'));


f. Using Middleware to Log Request Details

app.use((req, res, next) => {​


console.log(`${req.method} request to ${req.url}`);​
next();​
});
g. Serving HTML, CSS, and JavaScript Files​

app.use(express.static('public'));

h. Returning JSON Data from an API​

app.get('/json', (req, res) => {​


res.json({ message: 'Hello, this is JSON data!' });​
});​


You might also like