作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我目前正在开发一个使用 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)
应该有更好的方法。你会怎么做?
谢谢。
最佳答案
使用中间件功能。这样,robots.txt 将在任何 session 、cookieParser 等之前处理:
app.use('/robots.txt', function (req, res, next) {
res.type('text/plain')
res.send("User-agent: *\nDisallow: /");
});
使用 express 4 app.get
现在按照它出现的顺序进行处理,因此您可以使用它:
app.get('/robots.txt', function (req, res) {
res.type('text/plain');
res.send("User-agent: *\nDisallow: /");
});
关于node.js - 在 Express 中处理 robots.txt 的最聪明方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15119760/
我是一名优秀的程序员,十分优秀!