gpt4 book ai didi

node.js - 如果在 express.js 中既没有调用 res.send() 也没有调用 res.end() 会发生什么?

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:43 26 4
gpt4 key购买 nike

我有一个安全问题,有人试图调用随机 API,这些 API 在我们的服务器上不受支持,但通常经常用于管理员 API。我在下面设置这段代码来处理 404 以不响应此攻击

url-not-found-handler.js

'use strict';
module.exports = function () {
//4XX - URLs not found
return ((req, res, next) => {
});
};

客户端发生的事情是它等待服务器响应,但我想知道这是否会影响我的 express.js 服务器的性能以及在没有 res.send() 的情况下服务器的幕后发生了什么 还是 res.end()

最佳答案

根据 res.end() 的文档.

Ends the response process. This method actually comes from Node core, specifically the response.end() method of http.ServerResponse.

然后 response.end

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.


如果您让您的请求悬而未决,httpserver 肯定会保留有关它的数据。这意味着如果您让许多请求挂起,您的内存将会增加并降低服务器性能。

关于客户端,他将不得不等到请求超时。


处理错误请求的最佳方法是立即拒绝该请求,这会释放该请求所允许的内存。

你无法阻止错误的请求(也许有防火墙阻止来自某些 IP 地址的请求?)。您能做的就是尽快处理它们。

关于node.js - 如果在 express.js 中既没有调用 res.send() 也没有调用 res.end() 会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52850461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com