-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (33 loc) · 1.02 KB
/
Copy pathindex.js
File metadata and controls
39 lines (33 loc) · 1.02 KB
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
const fs = require("fs");
const http = require("http");
const { writeFile } = require("fs/promises");
const util = require("util");
const readFileAsync = util.promisify(fs.readFile);
const writeFileSync = util.promisify(fs.writeFile);
const readNotes = async () => {
try {
const data = await readFileAsync("notes.txt", "utf-8");
console.log(data);
} catch (error) {
console.error(`Internal Server Error: ${error.message}`);
}
};
const createNotes = async (fileName, data) => {
try {
await writeFileSync(fileName, data);
console.log(`File '${fileName}' created successfully.`);
} catch (error) {
res.status(500).json({
error: `Internal Server Error: ${error.message}`,
});
}
};
// readNotes();
// createNotes("note1.txt", "This is the content of the new file.");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello from Index.js");
});
server.listen(3000, () => {
console.log(`Server running at http://localhost:3000`);
});