schangxiang@126.com
2025-09-18 49a51c068d62084bc4c3e77c4be94a20de556c4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict';
 
const path = require('path');
const MAX_AGE = 'public, max-age=2592000'; // 30 days
 
module.exports = options => {
  return function siteFile(ctx, next) {
    if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return next();
    /* istanbul ignore if */
    if (ctx.path[0] !== '/') return next();
 
    const content = options[ctx.path];
    if (!content) return next();
 
    // '/favicon.ico': 'https://eggjs.org/favicon.ico',
    // content is url
    if (typeof content === 'string') return ctx.redirect(content);
 
    // '/robots.txt': Buffer <xx..
    // content is buffer
    if (Buffer.isBuffer(content)) {
      ctx.set('cache-control', MAX_AGE);
      ctx.body = content;
      ctx.type = path.extname(ctx.path);
      return;
    }
 
    return next();
  };
};