message.signal
当底层套接字关闭或请求被销毁时会中止的 <AbortSignal>。该信号在首次访问时才会懒惰创建——对于从未使用此属性的请求,不会分配任何 <AbortController>。
🌐 An <AbortSignal> that is aborted when the underlying socket closes or the request is destroyed. The signal is created lazily on first access — no <AbortController> is allocated for requests that never use this property.
当客户端在请求中途断开连接时,这对于取消下游异步工作(例如数据库查询或 fetch 调用)非常有用。
🌐 This is useful for cancelling downstream asynchronous work such as database
queries or fetch calls when a client disconnects mid-request.
import http from 'node:http';
http.createServer(async (req, res) => {
try {
const data = await fetch('https://example.com/api', { signal: req.signal });
res.end(JSON.stringify(await data.json()));
} catch (err) {
if (err.name === 'AbortError') return;
res.statusCode = 500;
res.end('Internal Server Error');
}
}).listen(3000);const http = require('node:http');
http.createServer(async (req, res) => {
try {
const data = await fetch('https://example.com/api', { signal: req.signal });
res.end(JSON.stringify(await data.json()));
} catch (err) {
if (err.name === 'AbortError') return;
res.statusCode = 500;
res.end('Internal Server Error');
}
}).listen(3000);