FSD Unit04 (Ques)
FSD Unit04 (Ques)
FSD Unit04 (Ques)
UNIT - 4
Syllabus : Basics of Node.js HTTP, Creating simple webs server, Inspecting headers, Request
and response, Serving base HTML, Serving a directory, Middleware, Create and use
middleware, HTTPs module.
Output:
FSD
User initiates a request: The user specifies a file name or directory and
the desired action (e.g., read, write, delete).
Request sent to file server: The request is sent to the file server.
File server interacts with file system: The file server queries the file
system based on the user's request. This might involve locating the file,
checking permissions, and preparing the data.
Response sent to user: The file server sends a response back to the user,
indicating success or failure and providing any necessary data (e.g., the
contents of a file).
FSD
// Start listening for incoming requests on port 8080 with error handling
server.listen(8080, (err) => {
if (err) {
console.error('Error starting server:', err);
// handle the error appropriately, e.g., exit process
FSD
} else {
console.log('An illustration to request & response !');
}
});
Output:
readableStream.pipe(writableStream);
readableStream.on('end', () => {
FSD
res.statusCode = 500;
res.end('Internal Server Error');
}
});
server.listen(8080, () => {
console.log('To understand http modules ');
});
Output:
FSD
http
const http = require('http');
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('<html><body><h1>Hello, World!</h1></body></html>');
});
server.listen(3000,'127.0.0.1',()=>{
});
We first require the http module, which comes built-in with Node.js.
We define the hostname and port where our server will listen for incoming requests. In this case, we're
using localhost (127.0.0.1) and port 3000.
We create an HTTP server using the http.createServer() method and pass in a request listener function.
This function will be called every time the server receives a request.
In the request listener function, we first set the HTTP status code to 200.
We then set the Content-Type header to text/html to indicate that we will be sending HTML content.
Finally, we end the response by sending the HTML content, which in this case is a simple <h1> heading
that says "Hello, World!".
We start the server by calling the server.listen() method and pass in the hostname and port that we
defined earlier.
When the server is running, we log a message to the console indicating that it is up and running.
To run the server, save the code to a file (e.g. server.js) and run it using the node command:
FSD
You should see the message "Server running at http://127.0.0.1:3000/" in the console.
To test the server, open a web browser and navigate to the URL "http://127.0.0.1:3000". You should
see the "Hello, World!" heading displayed in the browser.
Directory
Here is a basic example of serving a directory using Node.js and the http and fs (file system) modules
FSD
const fs = require('fs');
filePath = './index.html';
if (err) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
} else {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(data);
});
});
server.listen(8000,'127.0.0.1',()=>{
});
FSD
Mime
const filePaths = [
'index.html',
'style.css',
'script.js',
'image.png',
'document.pdf'
];
filePaths.forEach(filePath => {
});
Mimelookup
var http = require('http');
var path = require('path');
var fs = require('fs');
var mimelookup={'.js':'application/js','.html':'text/html'};
var server=http.createServer(function(req,res){
if(req.method=='GET'){
var fileurl;
if(req.url=='/')fileurl='/index.html';
else
fileurl=req.url;
var filepath=path.resolve('./public'+fileurl);
var fileExt=path.extname(filepath);
var mimeType=mimelookup[fileExt];
if(!mimeType){
send404(res);
return;
}
fs.exists (filepath,function(exists){
FSD
if(!exists){
send404(res);
return;
};
res.writeHead(200,{'content-Type':mimeType});
fs.createReadStream (filepath).pipe(res);
});
}
else{
send404(res);
}
});
server.listen(8000,'127.0.0.1',()=>{
console.log('server running at http://127.0.0.1:8000/');
});
FSD
Example :
const http = require('http');
console.log(`${header}: ${headers[header]}`);
});
server.listen(3000, () => {
});
Output: