在Express中处理robots.txt的最聪明方法是什么?


70

我目前正在使用Express(Node.js)构建的应用程序,我想知道在不同环境(开发,生产)下处理不同robots.txt的最聪明方法是什么。

这是我现在所拥有的,但是我对解决方案不满意,我认为它很脏:

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: /'

(注意:这是CoffeeScript)

应该有更好的方法。你会怎么做?

谢谢。

Answers:


105

使用中间件功能。这样,robots.txt将在任何会话,cookieParser等之前进行处理:

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

app.get现在,借助express 4可以按显示顺序处理它,因此您可以使用它:

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

1
当然,这样做app.use('/robots.txt', function (req, res, next) { ... });并失去req.url支票是有意义的。
c24w

1
@ c24w与快递4是的。app.get也会工作。我会更新。谢谢
SystemParadox

嗯,我认为这可能是API的新功能(我应该检查过)。 app.get更好!:)
c24w

3
此处的第二种方法更好,因为OP然后将其用于express <4。添加中间件意味着所有请求都通过该robots.txt检查。
flaviodesousa

1
@Fabian,只有res.sendFile()static中间件使用的文件扩展名。因为res.send()它会自动设置内容类型(如果尚未设置),但是对于字符串,它将设置为text / html。
SystemParadox

11

1.创建robots.txt具有以下内容:

User-agent: *
Disallow: # your rules here

2.将其添加到public/目录。

3.如果代码中尚不存在,请添加:

app.use(express.static('public'))

robots.txt可以在以下位置对任何爬虫使用http://yoursite.com/robots.txt


7
Express不会自动从中提供静态文件/public吗?我认为您需要对其进行配置。因此答案是不完整的。
伊恩·沃尔特

1
如果您的快速应用程序具有public/用于服务静态文件的目录;这个答案就像一个魅力。
Leandro Lima

这是可以在Express应用程序中完成的最简单,最明显的解决方案。我仍然不敢相信我为什么没有想到这一点。
彼得

2

看起来不错。

另一种选择是,如果您希望能够以robots.txt常规文件进行编辑,并且可能仅在生产或开发模式下拥有其他文件,则可以使用2个单独的目录,并在启动时激活一个或另一个。

if (app.settings.env === 'production') {
  app.use(express['static'](__dirname + '/production'));
} else {
  app.use(express['static'](__dirname + '/development'));
}

然后为每个版本的robots.txt添加2个目录。

PROJECT DIR
    development
        robots.txt  <-- dev version
    production
        robots.txt  <-- more permissive prod version

您可以继续在任一目录中添加更多文件,并使代码更简单。

(对不起,这是JavaScript,而不是coffeescript)


很有意思,我想我会尝试类似的事情,对我来说看起来更优雅!谢谢!
Vinch

只是想提一下,事情将会很快改变(Express 4.0)。你还需要“本土” .ENV然后按[process.env.NODE_ENV] :: scotch.io/bar-talk/...
sebilasse

0

这就是我在索引路由上所做的。您可以简单地在代码中写下我在下面给出的内容。

router.get('/', (req, res) =>
    res.sendFile(__dirname + '/public/sitemap.xml')
)

router.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/robots.txt')
})

0

我将robots.txt用作Prod的普通文件,并将其用作其他环境的中间件。

if(isDev || isStaging){
    app.use('/robots.txt', function (req, res) {
        res.type('text/plain');
        res.send("User-agent: *\nDisallow: /");
    });
}
app.use(express.static(path.join(__dirname, 'public')));

-1

使用中间件方式根据环境选择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();
    }
  });
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.