'render ejs template for robots.txt in express

I want to use some variables in my robots.txt file in Express.

How can I render this file as an EJS template? I already have EJS working for .html files.

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

Currently, I just get the the following error:

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)

The template is in ./app/views/robots.txt



Solution 1:[1]

I got it working by moving ./app/robots.txt to ./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
    });
};

Solution 2:[2]

I faced the issue, the solution I found is to use the npm module express-robots-txt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chovy
Solution 2 Cristik