0% found this document useful (0 votes)
9 views2 pages

Ex No 3

The document provides instructions for creating a simple Node.js server that serves static HTML and CSS files without using Express. It includes the necessary code to set up the server, handle file requests, and respond with appropriate status codes. The server listens on port 8080 and serves files from a 'public' directory, returning a 404 error for non-existent files.

Uploaded by

vanisris.mca
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)
9 views2 pages

Ex No 3

The document provides instructions for creating a simple Node.js server that serves static HTML and CSS files without using Express. It includes the necessary code to set up the server, handle file requests, and respond with appropriate status codes. The server listens on port 8080 and serves files from a 'public' directory, returning a 404 error for non-existent files.

Uploaded by

vanisris.mca
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/ 2

EX.

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

Command: npm install node

SERVER JS PAGE:
const http = require('http');
const fs = require('fs');
const path = require('path');

const port = 8080;


const publicDir = path.join(__dirname, 'public');

const server = http.createServer((req, res) => {


const filePath = path.join(publicDir, req.url);

fs.stat(filePath, (err, stats) => {


if (err || !stats.isFile()) {
res.writeHead(404, { 'Content-Type': 'text/html' });
return res.end('404 Not Found');
}

const fileStream = fs.createReadStream(filePath);

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.

*/

Run this JS page using below command in command prompt:

PS G:\FS\fullstack 2 and 3\EX.NO (3)> node server.js


Server is running on port 8080

Open the Browser and Run by searching http://localhost:8080/

OUTPUT:

You might also like