0

我有一个非常基本的节点脚本,它启动,提供一个 html 页面,最后关闭应用程序(理想情况下杀死节点进程)。一切都很好,除了最后一行调用 close 方法将其拆除。

var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app);

app.get('/', function(req, res) {
  res.sendfile(__dirname + '/index.html');
});

app.listen(8090);
console.log("started...");
app.close();

我在使用节点 0.8+ 运行此程序时遇到的错误是

app.close();
    ^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'

有谁知道我怎样才能更安全地关闭快递应用程序?

4

3 回答 3

2

你是如此接近:

var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app);

app.get('/', function(req, res) {
  res.sendfile(__dirname + '/index.html');
});

var listener = app.listen(8090);
console.log("started...");
listener.close();
于 2015-06-18T16:13:12.413 回答
1

到目前为止,我发现它是杀死节点进程的最佳方法。似乎完成了工作(尽管为了纯度我只想关闭快递)

process.exit(code=0)
于 2012-09-09T00:15:05.153 回答
0
process.on('SIGTERM', function () {
  app.close();
});

试试看

于 2012-09-07T18:32:58.340 回答