-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.mjs
72 lines (59 loc) · 2.62 KB
/
sample.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import fs from "fs"; // Importing file system
import path from "path"; // Importing path to create directory paths using 'join'
import express from "express"; // importing express
const server = express(); //creating server
const port = 5000; // defining potr
// Middleware to parse JSON bodies
server.use(express.json());
// Define the directory path { process.cwd() to take current working directory.then using Join we are creating path of the newly created folder.}
const dirPath = path.join(process.cwd(), "new_Folder");
// Create the directory if it does not exist. to aoid err happening due to same folder name
if (!fs.existsSync(dirPath)) {
try {
fs.mkdirSync(dirPath);
console.log("Directory created successfully!.");
} catch (error) {
console.error(`Error creating directory: ${error.message}`); // error handling
}
}
// POST endpoint to create a new file with content from the request body
server.post("/new-file", (req, res) => {
const timestap = new Date().toISOString(); // creating timestamp to put it inside the .txt file
const content = timestap;
// Generate a safe file name based on the current date and time. to avoid restricted punctuation symbols
const newFileName = new Date().toISOString().replace(/[:.]/g, "-");
const filePath = path.join(dirPath, `${newFileName}.txt`); // creating file path for newly created file
try {
fs.writeFileSync(filePath, content); // Write the content to the file
console.log("File created successfully");
res.status(201).send({ message: "File created successfully", filePath }); // response with HTTP codes 201 successful response
} catch (error) {
console.error(`Error writing file: ${error.message}`);
res.status(500).send({ error: "Error writing file" }); // response with HTTP codes 201 Error response
}
});
// GET endpoint to read all files in the folder and return their contents
server.get("/files", (req, res) => {
// read the file content
fs.readdir(dirPath, (err, files) => {
if (err) {
//handling error while reading
console.error("Error reading directory:", err);
return res.status(500).send("Error reading directory");
}
const fileContents = [];
files.forEach((file) => {
const filePath = path.join(dirPath, file);
// console.log(`Reading file: ${filePath}`);
const content = fs.readFileSync(filePath, "utf8"); // encode and decoding file content
fileContents.push({ file, content });
});
res.send({ files: fileContents });
});
});
// Start the server
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
// run server
// node sample.mjs