llms.txt لـ Express.js

Express هو إطار الويب الأكثر شعبية في Node.js. وتقديم llms.txt بسيط، إما عبر express.static() لملف ثابت أو عبر مسار GET مخصص للتوليد الديناميكي.

آخر تحديث:

الخيار 1: ملف ثابت باستخدام express.static()

إذا كان تطبيقك يستخدم بالفعل express.static() لتقديم public/ مجلد، أسقط ببساطة llms.txt داخل ذلك المجلد. وسيقدمه Express على /llms.txt من دون كود إضافي.

server.js, static file approach
// server.js
const express = require('express');
const app = express();

// Serve everything in ./public/ at the root URL.
// If public/llms.txt exists, it is available at /llms.txt automatically.
app.use(express.static('public'));

app.listen(3000, () => console.log('Listening on http://localhost:3000'));

// Project structure:
// your-express-app/
// ├── public/
// │   └── llms.txt   ← add this
// └── server.js

يضبط Express تلقائياً Content-Type إلى text/plain لـ .txt ملفات مقدّمة عبر express.static(). لإضافة Cache-Control في الترويسة، مرّر maxAge في الخيارات الثابتة:

Cache-Control with express.static()
app.use(express.static('public', { maxAge: '1h' }));

الخيار 2: مسار GET مخصص

للسيطرة الكاملة على الرؤوس والمحتوى، أضف مساراً مخصصاً. وهذا هو النهج المناسب أيضاً عندما يُنشأ المحتوى وقت التشغيل من قاعدة بيانات أو من سجل مسارات API لديك.

server.js, GET route
// server.js, dedicated GET route
const express = require('express');
const app = express();

const llmsContent = `# Your Site

> One-sentence description of what your site or product does.

## Documentation

- [Getting Started](https://yoursite.com/docs/start): Install and configure in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.

## Product

- [Overview](https://yoursite.com/product): Core features and capabilities.
- [Pricing](https://yoursite.com/pricing): Plans and billing details.

## Optional

- [Changelog](https://yoursite.com/changelog): Release history.
- [GitHub](https://github.com/your-org/your-repo): Source code.
`;

app.get('/llms.txt', (req, res) => {
  res.type('text/plain');
  res.set('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
  res.send(llmsContent);
});

app.listen(3000);

ضع هذا المسار قبل أي معالجات مسارات شاملة أو برمجية وسيطة لصفحات 404، وإلا فلن يصل Express إليه.

إصدار TypeScript

إذا كان مشروعك يستخدم TypeScript مع @types/express, اكتب معلمات المعالج صراحةً لتجنب any الأخطاء:

server.ts, TypeScript
// server.ts, TypeScript version with typed Request/Response
import express, { Request, Response } from 'express';

const app = express();

const llmsContent = `# Your Site

> One-sentence description of your product or service.

## Documentation

- [Getting Started](https://yoursite.com/docs/start): Quick setup guide.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog.

## Optional

- [Changelog](https://yoursite.com/changelog): Release history.
`;

app.get('/llms.txt', (req: Request, res: Response): void => {
  res.set({
    'Content-Type': 'text/plain; charset=utf-8',
    'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
  });
  res.send(llmsContent);
});

app.listen(3000, () => console.log('Server running on http://localhost:3000'));

إنشاء ديناميكي من بيانات وصف المسارات

بالنسبة إلى التطبيقات الأكبر، حافظ على publicRoutes مصفوفة إلى جانب تعريفات المسارات. و /llms.txt يحوّل المعالج هذه المصفوفة إلى روابط Markdown، مما يضمن بقاء الملف متزامناً مع مساراتك الفعلية.

server.js, dynamic generation
// server.js, generate llms.txt dynamically from route metadata
const express = require('express');
const app = express();

// Define your public routes with metadata
const publicRoutes = [
  { title: 'Getting Started', path: '/docs/start', description: 'Install and configure in minutes.' },
  { title: 'API Reference', path: '/docs/api', description: 'Full endpoint catalog with examples.' },
  { title: 'Authentication', path: '/docs/auth', description: 'OAuth 2.0 and API key setup.' },
  { title: 'Webhooks', path: '/docs/webhooks', description: 'Event payloads and retry policy.' },
  { title: 'Pricing', path: '/pricing', description: 'Plans and billing details.' },
];

const SITE_URL = process.env.SITE_URL || 'https://yoursite.com';

app.get('/llms.txt', (req, res) => {
  const links = publicRoutes
    .map((r) => `- [${r.title}](${SITE_URL}${r.path}): ${r.description}`)
    .join('\n');

  const body = [
    '# Your Site',
    '',
    '> One-sentence description of your product.',
    '',
    '## Documentation',
    '',
    links,
  ].join('\n');

  res.set({
    'Content-Type': 'text/plain; charset=utf-8',
    'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
  });
  res.send(body);
});

app.listen(3000);

اضبط SITE_URL كمتغير بيئة حتى يعمل الرمز نفسه عبر البيئات المحلية وبيئات التجهيز والإنتاج.

رأس Cache-Control

أضف دائماً Cache-Control ترويسة. مدة TTL ساعة واحدة مع stale-while-revalidate افتراض معقول؛ إذ يتيح للوكلاء العكسيين وشبكات CDN (nginx وCloudflare وAWS CloudFront) تخزين الاستجابة مؤقتاً وتقديم المحتوى القديم أثناء إعادة التحقق في الخلفية:

Recommended Cache-Control
res.set('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');

إذا كنت تستخدم CDN أمام Express، فتحقّق من أن CDN يحترم Cache-Control من المصدر. وتحترمه Cloudflare افتراضياً، بينما يتطلب AWS CloudFront سياسة تخزين مؤقت تسمح بمرور رؤوس المصدر.

تحقّق

بعد تشغيل الخادم، تحقّق من تقديم الملف بصورة صحيحة:

Local verification
# Check headers
curl -I http://localhost:3000/llms.txt
# Expected:
# HTTP/1.1 200 OK
# Content-Type: text/plain; charset=utf-8
# Cache-Control: public, max-age=3600

# Check content
curl http://localhost:3000/llms.txt | head -5
# Should print:  # Your Site

بعد النشر، أجرِ الفحص نفسه على عنوان URL المباشر، ثم الصقه في مدقّق llms.txt للتوافق الكامل مع المواصفة.

قائمة فحص قبل الشحن

  • الملف مقدَّم على /llms.txt مع 200 OK.
  • Content-Type: text/plain; charset=utf-8 تم ضبطه.
  • Cache-Control الرأس موجود.
  • عنوان H1 واحد بالضبط في أعلى الملف.
  • ملخص الاقتباس مباشرةً بعد H1.
  • كل عناوين URL مطلقة (https://).
  • يُسجّل المسار قبل أي معالجات شاملة أو معالجات 404.
  • يعيد المدقق بلا أخطاء: llmtxt.info/validator/

أدلة ذات صلة

المصادر