【发布时间】:2019-06-01 17:57:30
【问题描述】:
我试过用这个,但没用:app.disable("x-powered-by");
我读过这样的帖子:
how to remove X-Powered-By in ExpressJS
Can't get rid of header X-Powered-By:Express
我使用 "express": "^4.16.4" 作为后端。在前端我使用 "react": "^16.7.0" 单页应用。
更新
express.js 在端口 5000 react.js 在 3000 端口
当我尝试点击这个网址时,http://localhost:5000/api/product x-powered-by :express 不见了。
在我的反应应用程序中,当我尝试点击 API http://localhost:5000/api/product 时,它会再次显示 x-powered-by:express。
每次使用 API http://localhost:5000/api/product 这意味着 node.js/express 服务器我得到了x-powered-by : express
但是当我尝试console.log(app); 时,我得到了这个:
settings:
[0] { 'x-powered-by': false,
[0] etag: 'weak',
[0] 'etag fn': [Function: generateETag],
[0] env: 'development',
[0] 'query parser': 'extended',
[0] 'query parser fn': [Function: parseExtendedQueryString],
[0] 'subdomain offset': 2,
[0] 'trust proxy': false,
[0] 'trust proxy fn': [Function: trustNone],
[0] view: [Function: View],
[0] views: 'D:\\WEBSITE\\hammerstout_nodejs_client\\views',
[0] 'jsonp callback name': 'callback' } },
'x-powered-by': false, 这应该可行吗?
代码
import express from 'express';
import bodyParser from 'body-parser';
// import passport from 'passport';
import connection from './config/conn';
import { CategoryRoutes,ProductRoutes } from './modules';
import session from 'express-session';
const app = express();
app.disable("x-powered-by");
console.log(app);
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// app.use(passport.initialize());
app.use('/api/', [CategoryRoutes, ProductRoutes]);
const port = process.env.PORT || 5000;
app.listen(port, (err) => {
if(err){
console.log(err);
}else{
console.log(`Server running on port ! ${port}`);
}
});
【问题讨论】:
-
我在 v4.16.* 上遇到了同样的问题,但
app.disable("x-powered-by");现在似乎在 v4.17.1 中再次正常工作。 -
如果你认为你成功了,那不是因为 app.disable("x-powered-by");突然在某些版本的 Express 中工作。这是因为您正在查看您的应用程序没有代理。例如,如果您使用 React 及其代理来传递 API 流量,即使您在 Express 服务器上禁用了该代理,该代理也会添加标头。
标签: javascript node.js reactjs express