无法禁用X-Powered-By:Express

问题描述 投票:2回答:2

我尝试过使用它,但它不起作用: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”作为后端。在前端iam使用“react”:“^ 16.7.0”单页应用程序。

UPDATE

端口5000中的express.js在端口3000中的react.js

当我试图点击这个网址时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

Cant Disable 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}`);
    }

});
javascript node.js reactjs express
2个回答
2
投票

app.disable("x-powered-by");是在4.16.4中禁用自定义标头的正确方法。这是Express 4.16.4和节点10.14.2的一个工作示例:

const express = require('express');
const app = express();

app.disable("x-powered-by");
app.get('/', function(req, res) {
  res.status(200);
  res.send("hello\n\n");
  res.end();
});
app.listen(9876, function() {
  console.log('ready');
});

从命令行运行此命令,然后调用curl -i http://localhost:9876/会产生以下输出:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 7
ETag: W/"7-RYgBn9PSVn8wOBXbat/kibLuX5I"
Date: Mon, 07 Jan 2019 03:24:09 GMT
Connection: keep-alive

hello

1
投票

我的角度应用程序效果相同。我正在使用角度代理(最后是webpack-dev-server)来访问我的服务器(避免CORS问题)。

当我使用邮递员或浏览器访问我的服务器(在端口3000上)上的REST-API时,响应不包含'x-powered-by'标头。使用代理使用我的角度应用程序(在端口4200上)访问同一服务器会显示标题。

我的发现是:webpack-dev-server使用express作为基础;所以我假设“错误”标题来自代理服务器,而不是来自端口3000上的服务器。

© www.soinside.com 2019 - 2024. All rights reserved.