Files
node-js/index.js
2026-06-22 21:19:14 +03:00

27 lines
714 B
JavaScript

import http from "http";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const server = http.createServer((req, res) => {
const filePath = path.join(__dirname, "message.txt");
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Error reading file");
} else {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(data);
}
});
});
server.listen(8080, () => {
console.log("Server is running on port 3000");
});
lalal