【问题标题】:How to add robots.txt to a node application?如何将 robots.txt 添加到节点应用程序?
【发布时间】:2019-06-12 22:15:32
【问题描述】:

将 robots.txt 添加到节点应用程序的最佳、最先进和最新的方法是什么?

这可以在网络服务器级别更好地处理吗?

P.S:我使用 Mongo 作为 DB,使用 Nginx 作为 Web 服务器。

【问题讨论】:

    标签: javascript html node.js express


    【解决方案1】:

    使用中间件功能。这样,您就可以为生产和开发等不同环境处理不同的 robots.txt 文件。

    app.use('/robots.txt', function (req, res, next) {
        res.type('text/plain')
        res.send("User-agent: *\nDisallow: /");
    });
    

    您还可以在它们所属的环境的文件夹中提供文件。

    if (app.settings.env === 'production') {
      app.use(express['static'](__dirname + '/production')); // Uses the robots.txt from production folder
    } else {
      app.use(express['static'](__dirname + '/development')); // Uses the robots.txt from development folder
    }
    

    相关帖子:What is the smartest way to handle robots.txt in Express?

    【讨论】:

    • 我应该只创建 robots.txt 并将其添加到 public/ 目录吗?我读过 Express 不会自动从 /public 提供静态文件,是吗?我读到您需要对其进行配置才能这样做?
    • 是的,查找有关使用 express 创建服务器的简短教程。他们解释得很好。然后,了解如何提供静态文件。 Robot.txt 只是另一个可以用作静态文件的文件。
    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2016-09-04
    • 2015-03-27
    • 2017-11-19
    相关资源
    最近更新 更多