file system Error(fs) on deployement
Answered
Citrine Wagtail posted this in #help-forum

Citrine WagtailOP
import fs from "fs";
import path from "path";
export const runtime = "edge";
export async function GET(request: Request) {
const ip =
request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip");
if (!ip) {
console.error("Unable to get IP address");
return;
}
const filePath = path.join(process.cwd(), "ip_addresses.txt");
let content = fs.existsSync(filePath)
? fs.readFileSync(filePath, "utf-8")
: "";
const lines = content.split("\n").filter((line) => line.trim() !== "");
const ipIndex = lines.findIndex((line) => line.includes(ip));
if (ipIndex !== -1) {
const parts = lines[ipIndex].split("|");
const serialNumber = parts[0].trim();
const existingCount = parseInt(parts[1].trim().split(":")[1], 10);
const newCount = existingCount + 1;
const newLine =
lines[ipIndex] = newLine;
} else {
const newLine =
lines.push(newLine);
}
content = lines.join("\n");
fs.writeFileSync(filePath, content);
console.log(
return new Response(
}
i am able to write the file using fs but white building it on the vercel or any other hosting service i am getting this error, can you guys give me a solution for this
import path from "path";
export const runtime = "edge";
export async function GET(request: Request) {
const ip =
request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip");
if (!ip) {
console.error("Unable to get IP address");
return;
}
const filePath = path.join(process.cwd(), "ip_addresses.txt");
let content = fs.existsSync(filePath)
? fs.readFileSync(filePath, "utf-8")
: "";
const lines = content.split("\n").filter((line) => line.trim() !== "");
const ipIndex = lines.findIndex((line) => line.includes(ip));
if (ipIndex !== -1) {
const parts = lines[ipIndex].split("|");
const serialNumber = parts[0].trim();
const existingCount = parseInt(parts[1].trim().split(":")[1], 10);
const newCount = existingCount + 1;
const newLine =
${serialNumber}. IP: ${ip} | Count: ${newCount}
;lines[ipIndex] = newLine;
} else {
const newLine =
${lines.length + 1}. IP: ${ip} | Count: 1
;lines.push(newLine);
}
content = lines.join("\n");
fs.writeFileSync(filePath, content);
console.log(
IP address ${ip} logged
);return new Response(
IP address ${ip} logged.
);}
i am able to write the file using fs but white building it on the vercel or any other hosting service i am getting this error, can you guys give me a solution for this

14 Replies

Citrine WagtailOP
alright let me check

@joulev Remove the export const runtime = edge line

Citrine WagtailOP
yeah it worked thanks, can you help me with one more thing how can i post the api through discord webhook ?

@joulev What do you mean?

Citrine WagtailOP
i mean i have an api to post, how can i post it thorugh the webhook

@Citrine Wagtail i mean i have an api to post, how can i post it thorugh the webhook

Just send the fetch() to the discord webhook?
fetch(env.DISCORD_WEBHOOK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: "Hello, World!" }),
})
fetch(env.DISCORD_WEBHOOK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: "Hello, World!" }),
})

Citrine WagtailOP
alright okay thank you so much one more thing
welp it was fine locally but i am getting 500 error on deployed app

"use client";
import { useEffect } from "react";
export default function IPLogger() {
useEffect(() => {
const logIP = async () => {
try {
await fetch("/api/test", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
} catch (error) {
console.error("Error logging IP:", error);
}
};
logIP();
}, []);
return null;
}
this is where i am getting the api

@joulev Just send the fetch() to the discord webhook?
fetch(env.DISCORD_WEBHOOK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: "Hello, World!" }),
})

Citrine WagtailOP
can you help me with this one please

Citrine WagtailOP
is there a way i can check the txt file on the browser

Citrine WagtailOP
anyone guys please help me out ?

@Citrine Wagtail welp it was fine locally but i am getting 500 error on deployed app

Different issue, make a new post