Next.js Discord

Discord Forum

file system Error(fs) on deployement

Answered
Citrine Wagtail posted this in #help-forum
Open in Discord
Avatar
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 = ${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
Image
Answered by joulev
Remove the export const runtime = edge line
View full answer

14 Replies

Avatar
joulev
Remove the export const runtime = edge line
Answer
Avatar
Citrine WagtailOP
alright let me check
yeah it worked thanks, can you help me with one more thing how can i post the api through discord webhook ?
Avatar
joulev
What do you mean?
Avatar
Citrine WagtailOP
i mean i have an api to post, how can i post it thorugh the webhook
Avatar
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!" }),
})
Avatar
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
Image
"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
can you help me with this one please
Avatar
Citrine WagtailOP
is there a way i can check the txt file on the browser
Avatar
Citrine WagtailOP
anyone guys please help me out ?
Avatar
joulev
Different issue, make a new post