栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

进程被杀死后,如何正常关闭Express Server?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

进程被杀死后,如何正常关闭Express Server?

如果有人感兴趣,我会自己找到解决方案(很想听听评论中的反馈)。

我为服务器上打开的连接添加了一个侦听器,将对这些连接的引用存储在数组中。关闭连接后,将从阵列中将其删除。

当服务器被杀死时,每个连接都通过调用其

end
方法来关闭。对于某些浏览器(例如Chrome),这还不够,所以在超时后,我会调用
destroy
每个连接。

const express = require('express');const app = express();app.get('/', (req, res) => res.json({ ping: true }));const server = app.listen(3000, () => console.log('Running…'));setInterval(() => server.getConnections(    (err, connections) => console.log(`${connections} connections currently open`)), 1000);process.on('SIGTERM', shutDown);process.on('SIGINT', shutDown);let connections = [];server.on('connection', connection => {    connections.push(connection);    connection.on('close', () => connections = connections.filter(curr => curr !== connection));});function shutDown() {    console.log('Received kill signal, shutting down gracefully');    server.close(() => {        console.log('Closed out remaining connections');        process.exit(0);    });    setTimeout(() => {        console.error('Could not close connections in time, forcefully shutting down');        process.exit(1);    }, 10000);    connections.forEach(curr => curr.end());    setTimeout(() => connections.forEach(curr => curr.destroy()), 5000);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号