Express에서 robots.txt를 처리하는 가장 현명한 방법은 무엇입니까?
저는 현재 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는 세션, 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: /");
});
robots.txt
다음 콘텐츠로 만들기 :User-agent: * Disallow:
public/
디렉토리에 추가하십시오 .
당신은 robots.txt
에서 크롤러 사용할 수 있습니다.http://yoursite.com/robots.txt
괜찮은 방법처럼.
대안으로, 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
그리고 두 디렉토리에 더 많은 파일을 계속 추가하고 코드를 더 간단하게 수 있습니다.
(죄송합니다.이 coffeescript가 아닌 자바 펼쳐입니다)
이것이 내가 색인 경로에서 한 일입니다. 아래에 내가 한 일을 코드에 간단히 적을 수 있습니다.
router.get('/', (req, res) =>
res.sendFile(__dirname + '/public/sitemap.xml')
)
router.get('/', (req, res) => {
res.sendFile(__dirname + '/public/robots.txt')
})
미들웨어 방식으로 환경에 따라 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();
}
});
}
'ProgramingTip' 카테고리의 다른 글
목록을 varargs로 전달 (0) | 2020.12.08 |
---|---|
ImageMagick을 실행하여 다중 페이지 PDF의 첫 페이지 만 JPEG로 변환하는 방법은 무엇입니까? (0) | 2020.12.08 |
새로운 정적이란 무엇입니까? (0) | 2020.12.08 |
Ansible을 사용하여 원격 명령의 출력 표시 (0) | 2020.12.08 |
Interface Builder에서 Yosemite 스타일의 통합 도구 모음을 어떻게 만들 수 있습니까? (0) | 2020.12.08 |