Ex No 3
Ex No 3
NO: 3 Create a Node JS server that sever that serves static HTML and CSS files to the
user without using Express
INSTALLING MODULES:
NODE JS
SERVER JS PAGE:
const http = require('http');
const fs = require('fs');
const path = require('path');
res.writeHead(200);
fileStream.pipe(res);
});
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
/*
Explanations
The code you provided creates a simple Node.js server that serves static files from a directory named
"public". Here's what the output would be:
1.When you run this code, it will start the server on port 8080.
2.If you navigate to http://localhost:8080 in your web browser, the server will attempt to serve the
file located at ./public/index.html.
3.If the file exists, it will be served to the client with a status code of 200.
4.If the file does not exist or is not a regular file (e.g., it's a directory), the server will respond with a
404 Not Found status code and a simple "404 Not Found" message.
*/
OUTPUT: