【发布时间】:2019-06-12 22:15:32
【问题描述】:
将 robots.txt 添加到节点应用程序的最佳、最先进和最新的方法是什么?
这可以在网络服务器级别更好地处理吗?
P.S:我使用 Mongo 作为 DB,使用 Nginx 作为 Web 服务器。
【问题讨论】:
标签: javascript html node.js express
将 robots.txt 添加到节点应用程序的最佳、最先进和最新的方法是什么?
这可以在网络服务器级别更好地处理吗?
P.S:我使用 Mongo 作为 DB,使用 Nginx 作为 Web 服务器。
【问题讨论】:
标签: javascript html node.js express
使用中间件功能。这样,您就可以为生产和开发等不同环境处理不同的 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?
【讨论】: