The load takes too long and times out.(100dollers)

This is as the title says. Below is the index.js. Please suggest a solution.

import express from “express”;
import fs from “fs/promises”;
import path from “path”;
import { fileURLToPath } from “url”;
import { marked } from “marked”;
import serverless from “serverless-http”;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// …既存コード…
app.set(“views”, path.resolve(process.cwd(), ‘views’));
console.log(“Express views directory:”, app.get(“views”));
app.set(“view engine”, “ejs”);
app.use(express.static(path.resolve(process.cwd(), “public”)));
const articlesDir = path.resolve(process.cwd(), “articles”);
// …既存コード…
app.get(“/”, async (req, res) => {
try {
console.log(“GET / called”);
const files = await fs.readdir(articlesDir);
console.log(“files:”, files);
// …以下略…
const articleList = ;
for (const file of files) {
if (file.endsWith(“.md”)) {
const mdPath = path.join(articlesDir, file);
const mdContent = await fs.readFile(mdPath, “utf-8”);
console.log(“mdContent length:”, mdContent.length);
const lines = mdContent.split(/\r?\n/);
const titleMatch = lines[0].match(/^#\s*(.+)/);
const title = titleMatch ? titleMatch[1] : file.replace(“.md”, “”);
const description = lines[1] ? lines[1].trim() : “”;
articleList.push({
title: title,
slug: file.replace(“.md”, “”),
description: description
});
}
}
res.render(“index”, { articles: articleList });
} catch (err) {
console.error(“記事一覧の読み込みエラー:”, err);
res.status(500).send(“記事一覧の読み込み中にエラーが発生しました。”);
}
});
app.get(“/article/:slug”, async (req, res) => {
try {
const filePath = path.join(articlesDir, ${req.params.slug}.md);
const md = await fs.readFile(filePath, “utf-8”);
const html = marked.parse(md);
const titleMatch = md.match(/^#\s(.+)/);
const title = titleMatch ? titleMatch[1] : req.params.slug;
res.render(“articles”, {
title: title,
content: html
});
} catch (err) {
console.error(“記事読み込みエラー:”, err);
res.status(404).send(“記事が見つかりません”);
}
});
app.get(“/terms”, (req, res) => {
res.render(“terms”);
});
export default serverless(app);

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

There is no direct file system access in serverless environments (like Vercel Functions). Strategies for accessing files are here: How can I use files in Vercel Functions?

We have a few blog examples here: Find your Template

One alternative to file access is to use Github API to fetch markdown from your git repository directly, which has the advantage of not requiring a re-deploy in order to publish new content.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.