You are on page 1of 7

Assignment No.

Name – Lohade Om Manoj

Roll No - (SEDA IT C) (Roll No. 4)

1) Design a web page demonstrating file handling operations like open,


read, write, append copy, move, delete and rename using nodeJS.
File :- public/script.js

function readFile() {
const filePath = document.getElementById('filePath').value;
fetch(`/read?path=${filePath}`)
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

app.post('/write', (req, res) => {


const filePath = req.body.path;
const content = req.body.content || '';
fs.writeFile(filePath, content, (err) => {
if (err) {
res.send('Error writing to the file.');
} else {
res.send('File written successfully.');
}
});
});

function appendFile() {
const filePath = document.getElementById('filePath').value;
const content = document.getElementById('content').value;
fetch(`/append?path=${filePath}&content=${content}`, {
method: 'POST'
})
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

function copyFile() {
const sourcePath = document.getElementById('sourcePath').value;
const destinationPath = document.getElementById('destinationPath').value;
fetch(`/copy?source=${sourcePath}&destination=${destinationPath}`)
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

function moveFile() {
const sourcePath = document.getElementById('sourcePath').value;
const destinationPath = document.getElementById('destinationPath').value;
fetch(`/move?oldPath=${sourcePath}&newPath=${destinationPath}`)
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

function deleteFile() {
const filePath = document.getElementById('filePath').value;
fetch(`/delete?path=${filePath}`)
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

function renameFile() {
const oldPath = document.getElementById('oldPath').value;
const newPath = document.getElementById('newPath').value;
fetch(`/rename?oldPath=${oldPath}&newPath=${newPath}`)
.then(response => response.text())
.then(data => {
document.getElementById('output').innerText = data;
});
}

Server.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;

app.use(express.urlencoded({ extended: true }));


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

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


res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/read', (req, res) => {
const filePath = req.query.path;
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.send('Error reading the file.');
} else {
res.send(data);
}
});
});

app.post('/write', (req, res) => {


const filePath = req.body.path;
const content = req.body.content;
fs.writeFile(filePath, content, (err) => {
if (err) {
res.send('Error writing to the file.');
} else {
res.send('File written successfully.');
}
});
});
app.post('/append', (req, res) => {
const filePath = req.body.path;
const content = req.body.content||'';
fs.appendFile(filePath, content, (err) => {
if (err) {
res.send('Error appending to the file.');
} else {
res.send('Content appended successfully.');
}
});
});
app.get('/copy', (req, res) => {
const sourcePath = req.query.source;
const destinationPath = req.query.destination;
fs.copyFile(sourcePath, destinationPath, (err) => {
if (err) {
res.send('Error copying the file.');
} else {
res.send('File copied successfully.');
}
});
});
app.get('/move', (req, res) => {
const oldPath = req.query.oldPath;
const newPath = req.query.newPath;
fs.rename(oldPath, newPath, (err) => {
if (err) {
res.send('Error moving the file.');
} else {
res.send('File moved successfully.');
}
});
});
app.get('/delete', (req, res) => {
const filePath = req.query.path;
fs.unlink(filePath, (err) => {
if (err) {
res.send('Error deleting the file.');
} else {
res.send('File deleted successfully.');
}
});
});
app.get('/rename', (req, res) => {
const oldPath = req.query.oldPath;
const newPath = req.query.newPath;
fs.rename(oldPath, newPath, (err) => {
if (err) {
res.send('Error renaming the file.');
} else {
res.send('File renamed successfully.');
}
});
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});

Index.htm
l<!DOCTYPE html>
<html>
<head>
<title>Node.js File Handling Operations</title>
</head>
<body>
<h2>File Handling Operations</h2>

<label for="filePath">File Path:</label>


<input type="text" id="filePath" placeholder="Enter file path">

<label for="sourcePath">Source Path:</label>


<input type="text" id="sourcePath" placeholder="Enter source path">

<label for="destinationPath">Destination Path:</label>


<input type="text" id="destinationPath" placeholder="Enter destination path">

<label for="oldPath">Old Path:</label>


<input type="text" id="oldPath" placeholder="Enter old path">

<label for="newPath">New Path:</label>


<input type="text" id="newPath" placeholder="Enter new path">

<textarea id="content" placeholder="Content" rows="4"


cols="50"></textarea><br><br>

<button onclick="readFile()">Read File</button>


<button onclick="writeFile()">Write File</button>
<button onclick="appendFile()">Append File</button>
<button onclick="copyFile()">Copy File</button>
<button onclick="moveFile()">Move File</button>
<button onclick="deleteFile()">Delete File</button>
<button onclick="renameFile()">Rename File</button>

<p id="output"></p>

<script src="script.js"></script>
</body>
</html>
OUTPUT
File Operations

Reading

You might also like