如何使用节点设置和拆除快递?

时间:2012-09-07 17:59:51

标签: node.js express

我有一个非常基本的节点脚本启动,提供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();

我运行w / node 0.8+时得到的错误是

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

任何人都知道如何更安全地关闭快递应用程序?

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

到目前为止,我发现了杀死节点进程的最佳方法。似乎完成了工作(尽管我希望只是表达纯洁)

process.exit(code=0)

答案 2 :(得分:-1)

process.on('SIGTERM', function () {
  app.close();
});

试试