在express中渲染robots.txt的ejs模板

cho*_*ovy 1 robots.txt node.js express

我想在 Express 中使用 robots.txt 文件中的一些变量。

如何将此文件呈现为 EJS 模板?我已经让 EJS 可以处理 .html 文件。

app.route('/robots.txt')
    .get(index.robotstxt);


/**
 * Send robotstxt file
 */
exports.robotstxt = function (req, res) {
    res.type('text/plain');
    res.render('robots.txt', {
        home: config.home
    });
};


# robotstxt.org

User-agent: *
Disallow: /settings
Disallow: /account/
Allow: /

sitemap: <%= home %>/sitemap.xml
Run Code Online (Sandbox Code Playgroud)

目前,我刚刚收到以下错误:

Error: Cannot find module 'txt' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at new View (/Users/user/projects/app/node_modules/express/lib/view.js:43:49) at Function.app.render (/Users/user/projects/app/node_modules/express/lib/application.js:484:12) at ServerResponse.res.render (/Users/user/projects/app/node_modules/express/lib/response.js:777:7) at Object.exports.robotstxt [as handle] (/Users/user/projects/app/lib/controllers/index.js:29:6) at next_layer (/Users/user/projects/app/node_modules/express/lib/router/route.js:103:13) at Route.dispatch (/Users/user/projects/app/node_modules/express/lib/router/route.js:107:5)
Run Code Online (Sandbox Code Playgroud)

模板位于./app/views/robots.txt

cho*_*ovy 5

我通过./app/robots.txt搬到./app/views/robots.ejs

/**
 * Send robotstxt file
 */
exports.robotstxt = function (req, res) {
    res.type('text/plain');
    res.render('robots.ejs', { //use .ejs extension instead of .txt
        home: config.home
    });
};
Run Code Online (Sandbox Code Playgroud)