如何将我的sitemap.xml(仅此文件)的HTTP标头“ X-Robots-Tag”设置为“ noindex,跟随” express?
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
但是标头中没有'X-Robots-Tag',所以我认为它不起作用。
答案 0 :(得分:1)
如docs中所述,静态中间件接受一个options参数,您可以在其中设置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"
}
*/
函数以基于路径修改响应标头:
{{1}}
答案 1 :(得分:0)
您实际上是在为请求设置标题,下面的代码应该可以工作:
app.get('/sitemap.xml', (req, res) => {
res.setHeader('X-Robots-Tag', 'noindex, follow')
res.sendFile(`${__dirname}/sitemap.xml`)
})
对于app.get
,回调函数具有三个参数:
app.get('/sitemap.xml', (request, response, nextHandler) => {})
request
是来自客户端的请求对象。
response
是您将从服务器发送的响应,这是您实际要更改的内容。
{nextHandler
通过调用(nextHandler()
)将所有内容传递给您的下一个请求处理程序