【发布时间】:2021-02-27 13:39:40
【问题描述】:
我创建了一个这样的快递应用
const express = require('express')
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.post('/close', async (_, res) => {
res.status(200);
res.end();
app.close();
});
module.exports = app;
我在另一个模块中实例化它
const myApp = require('./app.js');
myApp.listen(port, () => {
console.log(`Started server on ${port}`);
});
我希望服务器在收到对/close 的发布请求时自行关闭。目前,我只收到app.close is not a function 错误。
我知道我可以像这样在外部关闭服务器
const server = myApp.listen(port, () => {
console.log(`Started server on ${port}`);
});
server.close();
但我想在对/close 的发布请求中关闭服务器,我该怎么做?
【问题讨论】:
-
此处接受的答案可能会有所帮助。 stackoverflow.com/questions/14515954/…
-
@ozgur 它仍然使用我的应用无权访问的
server实例 -
我现在明白了。可以使用事件来完成吗?
-
你考虑过使用
worker_threads吗? -
@AdamAzad no...
标签: javascript node.js express nodejs-server