如何使用 Express 将我的 sitemap.xml(且仅此文件)的 HTTP header “X-Robots-Tag”设置为“noindex,follow”?
sitemap.xml 位于我的应用程序的根目录。
这是我的 exress 配置的片段:
express/development.js
const express = require('express')
const webpack = require('webpack')
const cors = require('cors')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpackHotServerMiddleware = require('webpack-hot-server-middleware')
const config = require('./../webpack/webpack.development.config.js')
const app = express()
const compiler = webpack(config)
const PORT = process.env.PORT || 3000
app.use(cors())
app.use(express.static('build'))
app.use(express.static('public'))
...
我已经尝试过:
...
app.get('/sitemap.xml', res => {
res.setHeader('X-Robots-Tag', 'noindex, follow')
})
还有这个:
app.get('/sitemap.xml', res => {
res.set('X-Robots-Tag', 'noindex, follow')
})
另外,我如何查看标题是否已更新。
我尝试过:$curl -v localhost:3000/sitemap.xml
但是 header 中没有“X-Robots-Tag”,所以我认为它不起作用。
最佳答案
如 docs 中所述,静态中间件接受一个选项参数,您可以在其中设置 setHeaders
函数以根据路径修改响应 header :
const fs = require('fs');
const path = require('path');
const http = require('http');
const express = require('express');
const publicDirectoryPath = path.join(__dirname, './public');
// the following creates and a public directory in the script directory and populates it with some files (for the sake of this demo).
// create the public directory if it doesn't exist
if (!fs.existsSync(publicDirectoryPath)) {
fs.mkdirSync(publicDirectoryPath);
}
// create a sample sitemap.xml (for the sake of this demo)
const siteMapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>`;
fs.writeFileSync(path.join(publicDirectoryPath, './sitemap.xml'), siteMapXml);
// create a sample index.html
const indexHtml = `<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Demo</title>
<p>It works!</p>
</html>`;
fs.writeFileSync(path.join(publicDirectoryPath, './index.html'), indexHtml);
// setup and start the express server
const app = express();
const PORT = process.env.PORT || 3000;
app.use(
express.static(path.join(__dirname, "public"), {
setHeaders: (res, path) => {
if (path.endsWith("sitemap.xml")) {
res.setHeader("X-Robots-Tag", "noindex, follow");
}
}
})
);
app.listen(PORT);
// check response headers
const makeGetRequestAndPrintHeaders = (path) => {
const options = {
hostname: 'localhost',
port: PORT,
path,
method: 'GET'
};
const req = http.request(options, (res) => {
console.log(`GET ${path}`);
console.log(JSON.stringify(res.headers, null, 2));
});
req.end();
}
makeGetRequestAndPrintHeaders('/index.html');
makeGetRequestAndPrintHeaders('/sitemap.xml');
// example output
/*
GET /index.html
{
"x-powered-by": "Express",
"accept-ranges": "bytes",
"cache-control": "public, max-age=0",
"last-modified": "Wed, 13 Feb 2019 14:28:26 GMT",
"etag": "W/\"6b-168e74243aa\"",
"content-type": "text/html; charset=UTF-8",
"content-length": "107",
"date": "Wed, 13 Feb 2019 14:28:26 GMT",
"connection": "close"
}
GET /sitemap.xml
{
"x-powered-by": "Express",
"x-robots-tag": "noindex, follow",
"accept-ranges": "bytes",
"cache-control": "public, max-age=0",
"last-modified": "Wed, 13 Feb 2019 14:28:26 GMT",
"etag": "W/\"113-168e74243a9\"",
"content-type": "text/xml; charset=UTF-8",
"content-length": "275",
"date": "Wed, 13 Feb 2019 14:28:26 GMT",
"connection": "close"
}
*/
关于node.js - 如何使用 Express 设置我的 sitemap.xml 的 'X-Robots-Tag',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671430/