node.js 在 Express 中处理 robots.txt 的最聪明方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15119760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the smartest way to handle robots.txt in Express?
提问by Vinch
I'm currently working on an application built with Express (Node.js) and I want to know what is the smartest way to handle different robots.txt for different environments (development, production).
我目前正在开发使用 Express (Node.js) 构建的应用程序,我想知道针对不同环境(开发、生产)处理不同 robots.txt 的最智能方法是什么。
This is what I have right now but I'm not convinced by the solution, I think it is dirty:
这就是我现在所拥有的,但我不相信解决方案,我认为它很脏:
app.get '/robots.txt', (req, res) ->
res.set 'Content-Type', 'text/plain'
if app.settings.env == 'production'
res.send 'User-agent: *\nDisallow: /signin\nDisallow: /signup\nDisallow: /signout\nSitemap: /sitemap.xml'
else
res.send 'User-agent: *\nDisallow: /'
(NB: it is CoffeeScript)
(注意:它是 CoffeeScript)
There should be a better way. How would you do it?
应该有更好的方法。你会怎么做?
Thank you.
谢谢你。
回答by SystemParadox
Use a middleware function. This way the robots.txt will be handled before any session, cookieParser, etc:
使用中间件功能。这样,robots.txt 将在任何会话、cookieParser 等之前处理:
app.use('/robots.txt', function (req, res, next) {
res.type('text/plain')
res.send("User-agent: *\nDisallow: /");
});
With express 4 app.getnow gets handled in the order it appears so you can just use that:
使用 express 4app.get现在按照它出现的顺序进行处理,因此您可以使用它:
app.get('/robots.txt', function (req, res) {
res.type('text/plain');
res.send("User-agent: *\nDisallow: /");
});
回答by atul
Create
robots.txtwith following content :User-agent: * Disallow:add it to
public/directory.
robots.txt使用以下内容创建:User-agent: * Disallow:将其添加到
public/目录中。
your robots.txtwill be available to crawler at http://yoursite.com/robots.txt
您robots.txt可以在http://yoursite.com/robots.txt
回答by Pascal Belloncle
Looks like an ok way.
看起来是个不错的方法。
An alternative, if you'd like to be able to edit robots.txtas regular file, and possibly have other files you only want in production or development mode would be to use 2 separate directories, and activate one or the other at startup.
另一种方法是,如果您希望能够robots.txt像常规文件一样编辑,并且可能有其他您只需要在生产或开发模式下使用的文件,则可以使用 2 个单独的目录,并在启动时激活一个或另一个。
if (app.settings.env === 'production') {
app.use(express['static'](__dirname + '/production'));
} else {
app.use(express['static'](__dirname + '/development'));
}
then you add 2 directories with each version of robots.txt.
然后为每个版本的 robots.txt 添加 2 个目录。
PROJECT DIR
development
robots.txt <-- dev version
production
robots.txt <-- more permissive prod version
And you can keep adding more files in either directory and keep your code simpler.
并且您可以继续在任一目录中添加更多文件并使您的代码更简单。
(sorry, this is javascript, not coffeescript)
(对不起,这是javascript,不是coffeescript)
回答by Chan Myae Maung
This is what I did on my index routes. You can just simply write down in your codes what I does given down below.
这就是我在索引路由上所做的。你可以简单地在你的代码中写下我在下面给出的内容。
router.get('/', (req, res) =>
res.sendFile(__dirname + '/public/sitemap.xml')
)
router.get('/', (req, res) => {
res.sendFile(__dirname + '/public/robots.txt')
})
回答by fernandopasik
For choosing the robots.txt depending the environment with a middleware way:
使用中间件方式根据环境选择robots.txt:
var env = process.env.NODE_ENV || 'development';
if (env === 'development' ||?env === 'qa') {
app.use(function (req, res, next) {
if ('/robots.txt' === req.url) {
res.type('text/plain');
res.send('User-agent: *\nDisallow: /');
} else {
next();
}
});
}

