4 javascript node.js express reactjs
我试过使用它,但它没有用:app.disable("x-powered-by");
我已经阅读了这样的帖子:
如何在 ExpressJS 中删除 X-Powered-By
我使用 "express": "^4.16.4" 作为后端。在前端使用“react”:“^16.7.0”单页应用程序。
更新
端口 5000 中的 express.js 端口 3000 中的 react.js
当我尝试访问此 URL 时,http://localhost:5000/api/product x-powered-by :express不见了。
在我的 React 应用程序中,当我尝试点击 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' } },
Run Code Online (Sandbox Code Playgroud)
'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}`);
}
});
Run Code Online (Sandbox Code Playgroud)
小智 7
我的 angular 应用程序也有同样的效果。我正在使用角度代理(最终是 webpack-dev-server)来访问我的服务器(避免 CORS 问题)。
当我使用邮递员或浏览器访问服务器(端口 3000)上的 REST-API 时,响应不包含“x-powered-by”标头。使用代理使用我的 angular 应用程序(在端口 4200 上)访问同一台服务器会显示标题。
我的发现是:webpack-dev-server 使用 express 作为基础;所以我假设“错误”标头来自代理服务器,而不是来自端口 3000 上的服务器。
app.disable("x-powered-by");是在 express 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');
});
Run Code Online (Sandbox Code Playgroud)
从命令行运行它,然后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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5030 次 |
| 最近记录: |